0

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?

1
  • 1
    please first search your question in stack and then if not found your aks, put ypur question. Commented Apr 21, 2015 at 13:50

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

4 Comments

This is giving me the same result as the other person's code: [('332.466666667', 'Callum'), ('84.9444444443', 'John'), ('850.766666667', 'Chris profile.txt'), ('655.793939394', 'James profile.txt')]
sorted is not changing the value of the variable l, so you need to assign result given by that function to a new variable (or the same one)
I still don't quite understand. I've got it so that 'Comb = zip(Number, Name)' and then I changed your code to have 'Comb' instead of 'l'. Is this not correct or am I missing something?
Okay I've finally got it all sorted out, thanks a lot!!
3

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)

5 Comments

Using this I'm getting: [('332.466666667', 'Callum'), ('84.9444444443', 'John'), ('850.766666667', 'Chris profile.txt'), ('655.793939394', 'James profile.txt')]
On a python 2.6, I get the expected value; on 2.7 [which is what this is tagged as], I get what Callum gets.
@Foon: I think you tried different versions of the code, which has been fixed edited so as to match Tim Rijavec's correct solution. Both Python 2.6 and 2.7 should give the exact same result, as they compare strings in the same way.
@Zaaferani: This answer (now corrected) is the same at Tim Rijavec's: I think that it should be deleted, as it does not bring anything (but a waste of time for those who check it out). :)
@EOL , yes, I first answer to this question. Tim Rijavec said Almost correct @Zaaferani.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.