Sorting a list of objects by multiple attributes

2006-04-10 Thread Steve Bergman
Hi, I am trying to come up with a clean and simple way to sort a list of objects (in this case SQLObject instances) by multiple attributes. e.g. a Person object may have an age, a lastName, and a firstName. I'd like to be able to arbitrarily sort by any combination of those in any order. I woul

Re: Sorting a list of objects by multiple attributes

2006-04-10 Thread gry
For multiple keys the form is quite analogous: L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey)) I.e., just return a tuple with the keys in order from your lambda. Such tuples sort nicely. -- George Young -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a list of objects by multiple attributes

2006-04-10 Thread nghoffma
If you are lambda-phobic (as I am) this should also work for an arbitrary set of attributes: attrs = 'attr1 attr2 attr3'.split() sortlist = [[getattr(o,a) for a in attrs] + [o] for o in objects] sorted_objects = [x[-1] for x in sorted(sortlist)] -Noah -- http://mail.python.org/mailman/listinfo/

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Kent Johnson
gry@ll.mit.edu wrote: > For multiple keys the form is quite analogous: > >L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey)) > > I.e., just return a tuple with the keys in order from your lambda. > Such tuples sort nicely. In Python 2.5 you can do this with operator.attrgetter()

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Scott David Daniels
Kent Johnson wrote: > In Python 2.5 you can do this with operator.attrgetter(): > L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey')) Note: this is also available in Python 2.4 --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Kent Johnson
Scott David Daniels wrote: > Kent Johnson wrote: >> In Python 2.5 you can do this with operator.attrgetter(): >> L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey')) > > Note: this is also available in Python 2.4 No, the ability to specify more than one attribute name, making

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Raymond Hettinger
[George Young] >> For multiple keys the form is quite analogous: >> >> L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey)) [Noah] > If you are lambda-phobic (as I am) this should also work for an > arbitrary set of attributes: > > attrs = 'attr1 attr2 attr3'.split() > sortlist = [[getat

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Scott David Daniels
Kent Johnson wrote: > Scott David Daniels wrote: >> Kent Johnson wrote: >>> In Python 2.5 you can do this with operator.attrgetter(): >>> L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey')) >> >> Note: this is also available in Python 2.4 > > No, the ability to specify more th

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread nghoffma
I'm sure my avoidance of lambdas as due as much to laziness as adherence to principle. This is a good opportunity to learn about them. I suggested the above because it wasn't obvious to me how one would pass the arbitrary set of attributes to the lambda expression (and I envisioned them being spe

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Azolex
Raymond Hettinger wrote: > > The cult of lambda avoidance has lost contact with reality. [...] > Lambda avoidance is rooted in two things, an aversion to the keyword > name [...] Let's push the diagnosis a bit further : the aversion to the keyword "lambda" has to do with the fact that it ignore

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Raymond Hettinger
Azolex: > Let's push the diagnosis a bit further : the aversion to the keyword > "lambda" has to do with the fact that it ignores the english word used > by all non-geeks to convey the meaning, eg "given" Right. However, Guido has said that lambda is here to stay, so it's time to get over it. R

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Raymond Hettinger
[Noah] > I suggested the above because it wasn't obvious to me how one would > pass the arbitrary set of attributes to the lambda expression (and I > envisioned them being specified as strings in this case, since the set > of attributes will be coming from a web form). > > So what about the followi

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Azolex
Raymond Hettinger wrote: > Azolex: >> Let's push the diagnosis a bit further : the aversion to the keyword >> "lambda" has to do with the fact that it ignores the english word used >> by all non-geeks to convey the meaning, eg "given" > > Right. However, Guido has said that lambda is here to stay