How to sort this kind of list easily?

Ben Last ben at
Fri Aug 20 13:14:11 CEST 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.


More information about the Python-list mailing list