sorted([2, float('nan'), 1])
returns [2, nan, 1]
(At least on Activestate Python 3.1 implementation.)
I understand nan
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.
I asked a related question about max
, and based on that I understand why sort
works like this. But should this be considered a bug?
Documentation just says "Return a new sorted list [...]" without specifying any details.
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: http://connect.microsoft.com/VisualStudio/feedback/details/363379/bug-in-list-double-sort-in-list-which-contains-double-nan.
Anyway, I ended up following @khachik's answer:
sorted(list_, key = lambda x : float('-inf') if math.isnan(x) else x)
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).
nan
at least). Is it invalid in some other standard? - max