Sorted function in python
Since x[1]
is an integer, you can sort it from maximum to minimum simply by negating it:
sorted(unsorted_list, key=lambda x: (-x[1], x[0]))
The tuples created in key
will be sorted according to the first element (-x[1]
), then by second element (x[0]
). This corresponds exactly to your logic:
"So, it means than it is sorted by number but if numbers are the same the sort will be alphabetical."
In [2]: l = [['le', 5], ['aab', 4], ['aaa', 5]]
In [3]: sorted(l, key=lambda (x,y):(-y,x))
Out[3]: [['aaa', 5], ['le', 5], ['aab', 4]]