Sorting a multidimensional array by multiple keys

Thomas Krüger newsgroups at nospam.nowire.org
Mon Apr 2 02:28:58 EDT 2007


Alex Martelli schrieb:
> Thomas Krüger <newsgroups at nospam.nowire.org> wrote:
>> def sorter(a, b):
>>     return cmp(a.id, b.id)
>>
>> obj_lst.sort(sorter)
> 
> A MUCH better way to obtain exactly the same semantics would be:
> 
> def getid(a):
>     return a.id
> 
> obj_list.sort(key=getid)

Frankly speaking the purpose of the example was to show how to pass a
function as argument for the sort method.
Your code may be more efficient but it explains something different.

Thomas



More information about the Python-list mailing list