Bit of a python newbie, but I got the following list of tuples. I need to sort it by value and if the value is the same, solve ties alphabetically. Here's a sample:
#original
list_of_medals = [('Sweden', 24), ('Germany', 16), ('Russia', 10), ('Ireland', 10), ('Spain', 9), ('Albania', 8), ('Lithuania', 7), ('Iceland', 6), ('Malta', 5), ('Italy', 5), ('Serbia', 4), ('Estonia', 4), ('Turkey', 4), ('Moldova', 2), ('Azerbaijan', 2)]
# \____/ \_____/ \______/
#after sorting / \ / \ / \
sorted_medals = [('Sweden', 24), ('Germany', 16), ('Ireland', 10), ('Russia', 10), ('Spain', 9), ('Albania', 8), ('Lithuania', 7), ('Iceland', 6), ('Malta', 5), ('Italy', 5), ('Estonia', 4), ('Serbia', 4), ('Turkey', 4), ('Azerbaijan', 2), ('Moldova', 2)]
Is it perhaps possible with the operator
module?
In this instance, I'd use a lambda function as the
key
argument tosort()
/sorted()
:The negation of
x[1]
is needed to sort the medals in descending order while sorting country names in ascending order (simply settingreverse=True
wouldn't achieve that).As several people have pointed out in the comments, a more general way to do a complex sort on a compound key is to perform several sorting steps. To do this, sort on one component at a time, starting with the least significant one:
This relies on the fact that Python's sort is stable, meaning that items that compare equal are never reordered.
You can use the
sorted
function: