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 that will behave a lot like NaN
, with the semantics that if the returned object is Null
, it's actual value is unknown. A Null
object will also be able to interact with any other type of object (int
, float
, str
, bool
, etc), but any interactions will result in Null
.
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.
Of course, I could always dodge the issue and force the user to implement custom sort keys.
Any thoughts/advice/criticisms/etc?