Q: sort's key and cmp parameters

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Oct 7 09:38:58 CEST 2009


On Tue, 06 Oct 2009 23:20:13 -0700, Paul Rubin wrote:

> Bearophile <bearophileHUGS at lycos.com> writes:
>> sorting, and something that's surely not bug-prone. In such situation
>> having a 'key' argument is *better*. Such sort can be stable.
> 
> Nothing stops comparison sorting from being stable.  Since the rest of
> your post seems premised on the opposite, I hope that clears things up
> for you.

I'm sure Paul already knows this, but key-based sorts are comparison 
sorts.

There are two basic types of sorts: comparison based, where the routine 
has to compare items (usually with the < operator), and non-comparison 
sorts, like bucket sort, pigeon-hole sort and radix sort. These sorts 
require special knowledge of the items being sorted, and don't need to 
compare two items. General purpose sorts like Python's sort() do, 
regardless of whether you pass a key function, a three-way comparison 
function, or something else.




-- 
Steven



More information about the Python-list mailing list