Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18937

NaN, Null, and Sorting

Date 2012-01-13 11:04 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject NaN, Null, and Sorting
Newsgroups comp.lang.python
Message-ID <mailman.4725.1326484286.27778.python-list@python.org> (permalink)

Show all headers | View raw


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.

Thoughts/advice/criticisms/etc?

~Ethan~

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

NaN, Null, and Sorting Ethan Furman <ethan@stoneleaf.us> - 2012-01-13 11:04 -0800
  Re: NaN, Null, and Sorting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-14 04:54 +0000
  Re: NaN, Null, and Sorting jmfauth <wxjmfauth@gmail.com> - 2012-01-13 23:43 -0800
  Re: NaN, Null, and Sorting Eelco <hoogendoorn.eelco@gmail.com> - 2012-01-16 02:22 -0800
    Re: NaN, Null, and Sorting Chris Angelico <rosuav@gmail.com> - 2012-01-16 21:57 +1100
    Re: NaN, Null, and Sorting Robert Kern <robert.kern@gmail.com> - 2012-01-16 11:07 +0000

csiph-web