Reputation: 31
I've got an array in the format [number, name], and I'm trying to sort it from highest number to the lowest, using the sort() function.
I'm currently using sort() on the array and then reversing the outcome, however it doesn't seem to be sorting correctly.
An here are the values I'm using:
['850.766666667', 'Chris'], ['332.466666667', 'Callum'], ['655.793939394', 'James'], ['84.9444444443', 'John']
And this is the outcome I'm getting:
[('850.766666667', 'Chris'), ('84.9444444443', 'John'), ('655.793939394', 'James'), ('332.466666667', 'Callum')]
As you can probably see, ('84.9444444443', 'John')
should be at the end, however it is the second value after the sort.
What am I doing wrong?
Upvotes: 0
Views: 61
Reputation: 1780
Almost correct @Zaaferani
l = ['850.766666667', 'Chris'], ['332.466666667', 'Callum'], ['655.793939394', 'James'], ['84.9444444443', 'John']
sorted(l, key=lambda k: float(k[0]), reverse=True)
if you wan't to store the output of the sorted function
l=sorted(l, key=lambda k: float(k[0]), reverse=True)
Upvotes: 3
Reputation: 951
use this code
l = ['850.766666667', 'Chris'], ['332.466666667', 'Callum'], ['655.793939394', 'James'], ['84.9444444443', 'John']
sorted(l, key=lambda k: float(k[0]), reverse=True)
Upvotes: 3