Sorting a list of objects by multiple attributes

Steve Bergman steve at rueb.com
Mon Apr 10 21:19:37 EDT 2006


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 would especially like it to be clear and simple, since I am
converting a web app from using SQL to using an ORM only and so the
sorting will actually be controlled by variables coming from SELECTs in
a web form.  In SQL this is easy enough to accomplish, and I can do it
in python with L.sort(key=lambda i: i.whatever).  But for the multiple
attribute case, I'm at a bit of a loss.

Thanks for any help.

-Steve




More information about the Python-list mailing list