0

I read https://wiki.python.org/moin/HowTo/Sorting

>>> sorted(student_tuples, key=itemgetter(1, 0)) [('andrew', 14, 15), ('jane', 12, 10), (zach, 14, 12)]

I can sort in two ways but I want to sort in reverse order with the first parameter but not on the second so I get something like

[('andrew', 14, 15), ('zach', 14, 12), ('jane', 12, 10)]

How can I do that with the reverse=True parameter?

Currently if I use >>> sorted(student_tuples, key=itemgetter(1, 0), reverse=True) both of the sorting orders will be from the highest to lowest. I want only one to be from highest to lowest.

2
  • 2
    Your example is not clear. Can you please explain with a better example? Commented Mar 29, 2014 at 7:54
  • I updated it, is it still not clear? Commented Mar 29, 2014 at 8:34

1 Answer 1

1

Is this you are looking for:

>>> sorted(st, key=lambda x: (x[1]*-1,x[0]))
[('andrew', 14, 15), ('zach', 14, 12), ('jane', 12, 10)]
>>>
Sign up to request clarification or add additional context in comments.

1 Comment

I am looking for the combo of them both. SOrting by value of the second item first in descending order, then by the alphabetical order of the first.

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.