Sorting a multidimensional array by multiple keys

Thomas Krüger newsgroups at nospam.nowire.org
Sat Mar 31 08:53:11 EDT 2007


Rehceb Rotkiv schrieb:
> can I sort a multidimensional array in Python by multiple sort keys? A 
> litte code sample would be nice!

You can pass a function as argument to the sort method of a list.
The function should take two arguments and return -1, 0 or 1 as
comparison result. Just like the cmp function.

This will objects in list obj_lst by their id attributes:

def sorter(a, b):
    return cmp(a.id, b.id)

obj_lst.sort(sorter)

Thomas



More information about the Python-list mailing list