NaN, Null, and Sorting

Ethan Furman ethan at
Fri Jan 13 14:04:48 EST 2012

With NaN, it is possible to get a list that will not properly sort:

--> NaN = float('nan')
--> spam = [1, 2, NaN, 3, NaN, 4, 5, 7, NaN]
--> sorted(spam)
[1, 2, nan, 3, nan, 4, 5, 7, nan]

I'm constructing a Null object with the semantics that if the returned 
object is Null, it's actual value is unknown.

 From a purist point of view if it is unknown then comparison results 
are also unknown since the actual value might be greater, lesser, or the 
same as the value being compared against.

 From a practical point of view a list with Nulls scattered throughout 
is a pain in the backside.

So I am strongly leaning towards implementing the comparisons such that 
Null objects are less than other objects so they will always sort together.



More information about the Python-list mailing list