Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

18
  • 28
    For completeness from timeit: for me first gave 6 us per loop and the second 4.4 us per loop Commented Feb 8, 2013 at 21:52
  • 33
    Is there a way to sort the first one ascending and the second one descending? (Assume both attributes are strings, so no hacks like adding - for integers) Commented Aug 5, 2013 at 11:03
  • 98
    how about if I want to apply revrse=True only to x[1] is that possible ?
    – Amyth
    Commented Nov 24, 2014 at 11:45
  • 39
    @moose, @Amyth, to reverse to only one attribute, you can sort twice: first by the secondary s = sorted(s, key = operator.itemgetter(2)) then by the primary s = sorted(s, key = operator.itemgetter(1), reverse=True) Not ideal, but works. Commented Apr 13, 2015 at 9:43
  • 76
    @Amyth or another option, if key is number, to make it reverse, you can multiple it by -1.
    – Serge
    Commented Apr 6, 2016 at 11:21