How to sort a list? NOT a newbie question.

Michael James Barber mjbarber at
Tue Sep 18 11:09:39 CEST 2001

In article <mailman.1000739421.20070.python-list at>,
Skip Montanaro  <skip at> wrote:
[my question about sorting heterogeneous lists snipped]
>Seems to me that you need to convert complex numbers to reals for
>comparison.  That can be done a number of ways.  Some that come immediately
>to mind include:
>    * compare them as tuples, e.g., compare 1+2j as (1,2)
This is what I wound up using, actually.  For the particular case I was 
interested in, changing representations made sense and was easy to do.  
Naturally, comparing these alternate representations of complex numbers 
is every bit as incorrect mathematically as the direct comparison.

>Which you choose will depend on your application.
[example comparison function snipped]
There is a definite appeal to this approach.  Raise errors for "wrong" 
comparisons, and have the user define a convention as appropriate.  Of 
course, comparing non-numeric types is arguably wrong as well, but the 
lexicographical sort is so familiar that we don't really notice.  

Thanks for the thought-provoking comment.  I think I'm going to have to 
consider this very carefully before I decide if it's "obvious" or not!
					Michael J. Barber

More information about the Python-list mailing list