Guido rethinking removal of cmp from sort method

Dan Stromberg drsalists at gmail.com
Tue Mar 29 13:01:09 EDT 2011


On Tue, Mar 29, 2011 at 1:46 AM, Antoon Pardon <Antoon.Pardon at rece.vub.ac.be
> wrote:

> The double sort is useless if the actual sorting is done in a different
> module/function/method than the module/function/method where the order
> is implemented. It is even possible you didn't write the module
> where the sorting actually occurs.
>

There's a good point in the above.

Usually the order in which a class needs to be sorted, is a detail that
should be hidden by the class (or auxiliary comparison function).

Doing more than one sort in order to get one key forward and another in
reverse, seems to require a client of the class to know class detail (or
auxiliary comparison function detail).

In fact, if something inside the class (or auxiliary comparison function)
needs to change s.t. you must change how you sort its instances, then you
might have to change a bunch of single-sorts to multiple-sorts in client
code.  EG if you were sorting a number in reverse followed by a string
forward, to a number in reverse followed by a string forward and another
string in reverse.

In principle, you could come up with a "negate string" operation though.  EG
for Latin-1, each ch would become chr(255-ord(ch)).  That's probably going
to be rather expensive, albeit linear.  It should at least avoid the need
for multi-sorting and exposing implementation detail.  I want to say you
could do something similar for Unicode strings, but I don't have enough
experience with Unicode to be sure yet.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110329/35acff07/attachment-0001.html>


More information about the Python-list mailing list