I have a list of lists that contains users and scores as follows:
[["user1", 100], ["user2", 234], ["user3", 131]...]
I want to produce a list that sorts the users by score in declining order:
[["user2", 234], ["user3", 131], ["user1", 100]...]
How might I go about doing this kind of sort?
>>> li = [["user1", 100], ["user2", 234], ["user3", 131]]
>>>
>>> import operator
>>>
>>> sorted(li, key=operator.itemgetter(1)) # Ascending order
[['user1', 100], ['user3', 131], ['user2', 234]]
>>> sorted(li, key=operator.itemgetter(1), reverse=True) # Reverse Sort
[['user2', 234], ['user3', 131], ['user1', 100]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With