title: sort function breaks in the presence of NaN, except when it doesn't
body: <p>I'm experimenting with NaN values and it turns out that sorting tuples containing NaN doesn't work very well.</p>
  
  <pre><code>&gt;&gt;&gt; tuples = [(float('nan'), i) for i in range(7)]
  ... random.shuffle(tuples)
  ... sorted(tuples)
  [(nan, 6), (nan, 0), (nan, 2), (nan, 5), (nan, 4), (nan, 3), (nan, 1)]
  </code></pre>
  
  <p>This kinda makes sense, considering that all comparison operations between NaN and NaN should return False, as explained in <a href="https://stackoverflow.com/questions/4240050/python-sort-function-breaks-in-the-presence-of-nan">this question</a>.</p>
  
  <pre><code>&gt;&gt;&gt; float('nan') == float('nan') 
  False
  &gt;&gt;&gt; float('nan') &lt; float('nan') 
  False
  &gt;&gt;&gt; float('nan') &gt; float('nan') 
  False
  </code></pre>
  
  <p>However, when I change my example slightly, it is suddenly possible to sort the list of tuples.</p>
  
  <pre><code>&gt;&gt;&gt; nan = float('nan')
  ... tuples = [(nan, i) for i in range(7)]
  ... random.shuffle(tuples)
  ... sorted(tuples)
  [(nan, 0), (nan, 1), (nan, 2), (nan, 3), (nan, 4), (nan, 5), (nan, 6)]
  
  &gt;&gt;&gt; tuples #  it was really shuffled
  [(nan, 6), (nan, 0), (nan, 2), (nan, 3), (nan, 1), (nan, 4), (nan, 5)]
  </code></pre>
  
  <p>What's going on here? Why is it possible to sort the list in the second example, but not the first one?</p>