How to sort a list? NOT a newbie question.

Chris Barker chrishbarker at
Mon Sep 17 23:56:07 CEST 2001

>In Matlab,
> complex numbers are sorted based on their magnitudes, and then by phase
> angle if the magnitudes are the same.  Comparing complex numbers using <,
> <=, >, and >= always returns false.

Hmm. this isn't a solution that would be easy to use for Python, as
Python is more general purpose, so the list sort() method has to do the
same thing as the comparison operators. Also, you can write your own
sort function, so it would not be hard to deal with complex numbers
however you wanted.

Note that NumPy sort chokes on complex numbers as well, so I suppose you
could do something like MATLAB in Numpy.

One possible solution is for list.sort() have a way to deal with
"non-sortable" objects. I've had this problem when sorting a set of
two-tuples, when the second item in the tuple was not sortable.
list.sort() would choke, even though I was happy for it to ignore the
second item.



Christopher Barker,
ChrisHBarker at                 ---           ---           --- ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------

More information about the Python-list mailing list