How to sort a list? NOT a newbie question.

Toby Dickenson tdickenson at devmail.geminidataloggers.co.uk
Tue Sep 18 14:12:55 CEST 2001


Greg Ewing <greg at cosc.canterbury.ac.nz> wrote:

>> But what would the point be? The current implementation is telling you
>> that there is no meaningful sort order that it can impose.
>
>The point would be to make sort() impose an ordering on
>everything, arbitrary where necessary. That's what the
>original poster was asking for, and I was pointing out
>that the new rich-comps stuff doesn't make it impossible.
>
>Whether it's a good idea or not is another question.
>Personally I can't see how it would hurt anything, and
>it can sometimes be useful.

I like the C++ solution to this issue. A class can define two
different sorting orders for its instances...

Classes can overload the comparison operators to define the 'natural'
sorting order. The complex type doesnt do this, because there is no
'natural' ordering.

Seperately, classes can provide the cmp<> template function to define
a potentially arbitrary ordering. cmp<> can be used by data structures
that need to sort their content, but dont mind if the sort order is
not meaningful (for example, a C++ map<> container is similar to a
python dictionary, but uses a sorted tree of keys rather than hashes)

>But if you want to be purist about it, I'd suggest that
>sort() should refuse to compare objects of different types,
>either. Presently it's quite happy to impose an arbitrary
>ordering in that case.
>
>Greg Ewing, Computer Science Dept, +--------------------------------------+
>University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
>Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
>greg at cosc.canterbury.ac.nz	   +--------------------------------------+


Toby Dickenson
tdickenson at geminidataloggers.com



More information about the Python-list mailing list