title: Python: sort function breaks in the presence of nan
body: <p><code>sorted([2, float('nan'), 1])</code> returns <code>[2, nan, 1]</code></p>
  
  <p>(At least on Activestate Python 3.1 implementation.)</p>
  
  <p>I understand <code>nan</code> is a weird object, so I wouldn't be surprised if it shows up in random places in the sort result. But it also messes up the sort for the non-nan numbers in the container, which is really unexpected.</p>
  
  <p>I asked a <a href="https://stackoverflow.com/questions/4237914/python-max-min-builtin-functions-depend-on-parameter-order">related question</a> about <code>max</code>, and based on that I understand why <code>sort</code> works like this. But should this be considered a bug?</p>
  
  <p>Documentation just says "Return a new sorted list [...]" without specifying any details.</p>
  
  <p>EDIT: 
  I now agree that this isn't in violation of the IEEE standard. However, it's a bug from any common sense viewpoint, I think. Even  Microsoft, which isn't known to admit their mistakes often, has recognized this one as a bug, and fixed it in the latest version: <a href="http://connect.microsoft.com/VisualStudio/feedback/details/363379/bug-in-list-double-sort-in-list-which-contains-double-nan" rel="noreferrer">http://connect.microsoft.com/VisualStudio/feedback/details/363379/bug-in-list-double-sort-in-list-which-contains-double-nan</a>.</p>
  
  <p>Anyway, I ended up following @khachik's answer:</p>
  
  <pre><code>sorted(list_, key = lambda x : float('-inf') if math.isnan(x) else x)
  </code></pre>
  
  <p>I suspect it results in a performance hit compared to the language doing that by default, but at least it works (barring any bugs that I introduced).</p>