Sort

Greg Jorgensen gregj at pobox.com
Wed Dec 6 06:58:52 EST 2000


"Alfred" <scj at mcom.mcom.fr> wrote:

> I'm trying to sort a List of List, on the third col, but I don't find how
to
> do this.

>>> def itemcmp(a, b):
        return cmp(a[2], b[2])

>>> t = [ ['a',2,4], ['g',1,7], ['b',0,2], ['z',8,5] ]
>>> t.sort(itemcmp)
>>> t
[['b', 0, 2], ['a', 2, 4], ['z', 8, 5], ['g', 1, 7]]
>>>

The itemcmp function takes two elements (in this case, lists of at least 3
elements) and compares the third element using the built-in cmp function.

--
Greg Jorgensen
Deschooling Society
Portland, Oregon, USA
gregj at pobox.com





More information about the Python-list mailing list