[Tutor] sorting objects on two attributes

Kent Johnson kent37 at tds.net
Tue Mar 4 13:27:26 CET 2008


Here is a version based on Andreas' solution using groupby() that avoids 
the decorate-undecorate by using the key= parameter for the second sort:

l.sort(key=lambda x: (x.content_type, x.submit_date))
t = [ list(items) for key, items in itertools.groupby(l, key=lambda x:
(x.content_type)) ]
t.sort(key=lambda x: x[-1].submit_date, x[-1].object_id)

Kent


More information about the Tutor mailing list