How to sort this kind of list easily?

Ben Last ben at benlast.com
Fri Aug 20 07:14:11 EDT 2004


> 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




More information about the Python-list mailing list