Thanks, the problem is sorting. Say we have (a, 10), (b, 5), (c, 11). Need an algorithm to sort it to (c, 11), (a, 10), (b,5)
Alright, I assume you have these tuples in a list. You can use the built-in sorted
function and give it a key function which specifies the property by which the elements of your list are sorted.
>>> people = [('bob', 28), ('alice', 21), ('jeff', 78)]
>>> sorted(people, key=lambda tup: tup[1], reverse=True)
[('jeff', 78), ('bob', 28), ('alice', 21)]
Here lambda tup: tup[1]
will return the second element of each tuple. This is the value by which the tuples will be sorted. reverse=True
is set to sort the tuples in descending order.