Sorting on multiple values, some ascending, some descending

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Jan 3 20:18:11 EST 2007


dwelden wrote:
> >    L.sort(key=lambda r: r.secondary, reverse=True)
> >    L.sort(key=lambda r: r.primary)
> Excellent! That looks just like what I needed.

Note that there is the (probably little used) operator.attrgetter()
too, with that you can avoid the possibly slow lambda:
L.sort(key=attrgetter("primary")

operator.itemgetter(n) is when you have items that can be be accessed
by index.

Bye,
bearophile




More information about the Python-list mailing list