user12632313
user12632313

Reputation:

sort list so that the argument with the highest number is first

I want to sort my list so that the argument with the highest number is first

my_list = ["George 5", "Jonathan 2", "Kyle 11"]

print(my_list.sort())

desired output: ["Kyle 11", "George 3", "Jonathan 2"]

Upvotes: 2

Views: 55

Answers (1)

Aplet123
Aplet123

Reputation: 35522

You can sort with a key function, where the key splits on spaces, gets the last chunk (which should be the number), then parses it as a number:

my_list.sort(key=lambda x: int(x.split()[-1]), reverse=True)

Upvotes: 3

Related Questions