Guido rethinking removal of cmp from sort method

MRAB python at mrabarnett.plus.com
Tue Mar 29 14:57:21 EDT 2011


On 29/03/2011 18:01, Dan Stromberg wrote:
>
> On Tue, Mar 29, 2011 at 1:46 AM, Antoon Pardon
> <Antoon.Pardon at rece.vub.ac.be <mailto: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.
>
You would have to do more than that.

For example, "" < "A", but if you "negate" both strings you get "" <
"\xBE", not "" > "\xBE".



More information about the Python-list mailing list