sorting list of tuples by second (third...) tuple item

Justin Sheehy justin at
Fri Feb 15 15:27:42 CET 2002

kp87 at (kevin parks) writes:

> How would you sort something based on 2 criteria. Say you have a list
> of lists (i hate tuples). first thing you want is everything sorted by
> the first value and then everything that has the same value for [0]
> then sorted by [1]

This is the default behavior of Python's listobject's sort method.

It will sort by the first element, then the second, then the third, etc...

If you want some other order, you have two choices:

 - write a custom cmp function to pass to sort

 - decorate your list so that the default sort does what you want



More information about the Python-list mailing list