Sorting a list of objects by multiple attributes

nghoffma nghoffma at gmail.com
Tue Apr 11 01:11:09 EDT 2006


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




More information about the Python-list mailing list