Sorting NaNs

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jun 2 06:40:48 EDT 2018


On Sat, 02 Jun 2018 09:32:05 +0200, Peter J. Holzer wrote:

> Also nope. It looks like NaNs just mess up sorting in an unpredictable
> way. Is this the intended behaviour or just an accident of
> implementation? (I think it's the latter: I can see how a sort algorithm
> which doesn't treat NaN specially would produce such results.)


Neither -- it is a deliberate decision to ignore the possibility of such 
pathological objects when sorting. Feature requests to reconsider this 
decision in statistics.median will be considered seriously :-)

NANs are unordered values. ALL of these ought to return False, for ANY 
float value x:

    NAN < x
    NAN <= x
    NAN > x
    NAN >= x
    NAN == x

Regardless of the value of x, NAN <comparison> x is always false (apart 
from != which is always true).

Python's sort algorithm assumes that objects have consistent, sensible 
comparisons. If you write an object like this:


    class Rubbish:
        def __eq__(self, other):
             return random.random() > 0.5
        def __lt__(self, other):
             return random.random() > 0.5
        def __gt__(self, other):
             return random.random() > 0.5

it too will mess up sorting in unpredictable ways. So don't do that.


-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list