python 3: sorting with a comparison function

Terry Reedy tjreedy at udel.edu
Thu Oct 9 17:44:56 EDT 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?

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.




More information about the Python-list mailing list