Timeline for Sort a list by multiple attributes?
Current License: CC BY-SA 3.0
24 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 3, 2023 at 20:02 | comment | added | Nico Müller |
If you want to sort by a boolean value and reverse you can use not, also when sorting by multiple attributes k: (not k['has_paid'], k['name'])
|
|
Aug 25, 2022 at 15:41 | comment | added | Mr. Lance E Sloan |
I think the difference is that itemgetter() is a compiled Python library function, but using lambda creates a new function that needs to be interpreted.
|
|
Jun 9, 2020 at 19:15 | comment | added | Jethro Cao |
could someone please explain how using the callable created from functions in the operator module avoids a function call compared to using a lambda ? I mean using something like operator.itemgetter is still a function call, right?
|
|
Feb 2, 2020 at 17:57 | review | Suggested edits | |||
Feb 2, 2020 at 20:24 | |||||
Dec 22, 2019 at 16:10 | comment | added | Markus von Broady | @eyildiz multiplying by -1 doesn't work for string data | |
May 21, 2019 at 14:13 | comment | added | ron_g |
@Amyth another way to use reverse=True is to wrap both sorting criteria in parenthesis: s = sorted(s, key=lambda i: ( criteria1(i), criteria2(i) ), reverse=True)
|
|
Dec 6, 2018 at 22:53 | review | Suggested edits | |||
Dec 7, 2018 at 0:01 | |||||
May 8, 2018 at 9:31 | comment | added | eyildiz | @Serge +1 and not only works for int also boolean and string data = sorted(data, key=lambda x: (x['active']*-1,x['name'])) | |
Apr 24, 2018 at 22:05 | history | edited | smci | CC BY-SA 3.0 |
added 52 characters in body
|
Apr 24, 2018 at 22:03 | comment | added | smci |
You should say that key = operator.itemgetter(...) is much faster than key = lambda: ... , and move it to the top. Also, lambda is unnecessary rather than itemgetter if the custom sort is only accessing multiple fields, and not transforming them (e.g. negation, lowercase, custom order, ASCIIbetical order, a chain of object method calls etc.)
|
|
Jan 29, 2018 at 23:11 | comment | added | sara |
i've a list of array and i used this linesOrderedLeft = lines_Left.sort(key= lambda t: (t[0][1],t[0][3]) ) but it doesn't work with me as i am trying to sort the arrays based on their first and third index by summing them up So what is the problem with my code
|
|
Oct 26, 2017 at 15:25 | comment | added | Brad Solomon | To @Amyth's comment on negating, see related question here. | |
Apr 6, 2016 at 11:21 | comment | added | Serge |
@Amyth or another option, if key is number, to make it reverse, you can multiple it by -1 .
|
|
Apr 13, 2015 at 15:40 | comment | added | Martin Thoma |
@tomcounsell I think sorting twice is the best strategy in theis scenario as sorted is guaranteed to be stable (source)
|
|
Apr 13, 2015 at 9:43 | comment | added | tomcounsell |
@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.
|
|
Nov 24, 2014 at 11:45 | comment | added | Amyth |
how about if I want to apply revrse=True only to x[1] is that possible ?
|
|
Aug 6, 2013 at 14:09 | comment | added | Mark Byers | @moose: Unfortunately I don't think there is a non-hack way. The method proposed in the documentation is to sort twice, using the property that sorting is stable. See here for more details. | |
Aug 5, 2013 at 11:03 | comment | added | Martin Thoma |
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)
|
|
Feb 8, 2013 at 21:52 | comment | added | Brian Larsen | For completeness from timeit: for me first gave 6 us per loop and the second 4.4 us per loop | |
Nov 20, 2010 at 15:49 | vote | accept | headache | ||
Nov 20, 2010 at 15:38 | history | edited | Mark Byers | CC BY-SA 2.5 |
added 40 characters in body
|
Nov 20, 2010 at 15:38 | comment | added | Mark Byers |
@headache: I don't know which is faster - I suspect that they are about the same. You can use the timeit module to measure the performance of both if you are interested.
|
|
Nov 20, 2010 at 15:35 | comment | added | headache | You learn something new everyday! Do you know if this is computationally quicker than the previous method? Or does it just do the same thing in the background? | |
Nov 20, 2010 at 15:32 | history | answered | Mark Byers | CC BY-SA 2.5 |