Let's say I have a list as shown below:
[['Harry', '4'], ['Anthony', '10'], ['Adam', '7'], ['Joe', '6'], ['Ben', '10']]
I want to sort it by:
[['Anthony', '10'], ['Ben', '10'], ['Adam', '7'], ['Joe', '6'], ['Harry', '4']]
So, first sort it in descending order by count, and then sort it in ascending order by name.
I tried:
>>> sorted(l, key=lambda x: (int(x[1]), x[0])) [['Harry', '4'], ['Joe', '6'], ['Adam', '7'], ['Anthony', '10'], ['Ben', '10']]
This works, so now I just need to change it:
>>> sorted(l, key=lambda x: (int(x[1]), x[0]), reverse=True) [['Ben', '10'], ['Anthony', '10'], ['Adam', '7'], ['Joe', '6'], ['Harry', '4']]
Ah, reverse=True just changed the list, but did not give the result of the wait. So I just want to change the output of int(x[1]) , but not x[0] .
How can i do this?