python 3: sorting with a comparison function

Thomas Heller theller at
Fri Oct 10 12:06:37 CEST 2008

> Thomas Heller wrote:
>> Does Python 3 have no way anymore to sort with a comparison function?
>> Both [].sort() and sorted() seem to accept only 'key' and 'reverse' arguments,
>> the 'cmp' argument seems to be gone.  Can that be?

Terry Reedy schrieb:

> Yes.  When this was discussed, no one could come up with an actual use 
> case in which the compare function was not based on a key function. 
> Calling the key function n times has to be faster than calling a compare 
> function n to O(nlogn) times with 2 keys computed for each call.  The 
> main counter argument would be if there is no room in memory for the 
> shadow array of key,index pairs.  And that can be at least sometimes 
> handled by putting the original on disk and sorting an overt key,index 
> array.  Or by using a database.

bearophileHUGS at schrieb:

> Yes, that's a wonderful thing, because from the code I see around
> 99.9% of people see the cmp and just use it, totally ignoring the
> presence of the 'key' argument, that allows better and shorter
> solutions of the sorting problem. So removing the cmp is the only way
> to rub the nose of programmers on the right solution, and it goes well
> with the Python "There should be one-- and preferably only one --
> obvious way to do it.".

Thanks, I got it now.


More information about the Python-list mailing list