how to use sorted key python multiple elements code example

Example 1: sort by multiple keys in object python

s = sorted(s, key = lambda x: (x[1], x[2]))

Example 2: python get dictionary keys sorted by value

sorted(A, key=A.get)

Example 3: python sorted dictionary multiple keys

mylist = sorted(mylist, key=itemgetter('name', 'age'))
mylist = sorted(mylist, key=lambda k: (k['name'].lower(), k['age']))
mylist = sorted(mylist, key=lambda k: (k['name'].lower(), -k['age']))

Example 4: python sort based on multiple keys

>>> def multisort(xs, specs):
...     for key, reverse in reversed(specs):
...         xs.sort(key=attrgetter(key), reverse=reverse)
...     return xs