Possible Duplicate:
How to write Python sort key functions for descending values
In Python 3, it's pretty easy to sort a list of objects lexicographically using multiple keys. For example:
items.sort(key = lambda obj: obj.firstname, obj.lastname)
The reverse
argument lets you specify whether you want ascending or descending order. But what do you do in the case where you want to sort by multiple keys, but you want to sort using descending order for the first key, and ascending order for the second?
For example, suppose we have an object with two attributes, points
and name
, where points
is an int
and name
is a str
. We want to sort a list of these objects by points
in descending order (so that the object with the greatest number of points comes first), but for objects with an equal number of points
, we want to sort these by name
in alphabetical (ascending) order.
How can this be achieved?