How to sort this kind of list easily?

Scott David Daniels Scott.Daniels at Acm.Org
Sun Aug 22 15:02:43 EDT 2004


Ben Last wrote:
>>From: Anthony Baxter, in response to BruceKL WhoH
>>list.sort sorts tuples by first argument, then second &c. If you want a
>>custom sort order, look at the decorate-sort-undecorate pattern.
> 
> 
> Or if you want to sort only on your Id, use a lambda:
> l.sort(lambda x,y: cmp(x[0],y[0]))
> 
> To force sorting on just the nth element of the tuples, replace 0 with n in
> the above.
> 
> b
> 
If you are using 2.4 (now in beta), you could also do:

lst.sort(key=lambda element:element[1])



More information about the Python-list mailing list