Secondary list sorting comparison expression
David Pratt
fairwinds at eastlink.ca
Tue Mar 8 15:15:36 EST 2005
Hi Raymond. I appreciate your reply. Yes, this is exactly what I was
looking for. The syntax I had been trying to work out myself was not
correct and not giving me the right thing. Many thanks for your help -
this works just the way I wanted.
David
On Tuesday, March 8, 2005, at 03:46 PM, Raymond Hettinger wrote:
> Another way is to build out the sort_by_key function to handle
> multiple fields:
>
> def sort_by_key(list, i, j, k):
> list.sort(lambda a, b: cmp((a[i], a[j], a[k]), (b[i], b[j],
> b[k])))
>
> In Py2.4, the key= option offers an alternative to cmp which is
> simpler and
> faster:
>
> def sort_by_key(list, i, j, k):
> list.sort(key = lambda a : (a[i], a[j], a[k]))
More information about the Python-list
mailing list