[Tutor] sorting objects on two attributes
Kent Johnson
kent37 at tds.net
Mon Mar 3 17:06:08 CET 2008
Andreas Kostyrka wrote:
> l.sort(key=lambda x: (x.content_type, x.submit_date))
>
> Now, you can construct a sorted list "t":
>
> t = []
> for key, item_iterator in itertools.groupby(l, key=lambda x: (x.content_type, x.submit_date)):
> sorted_part = sorted(item_iterator, key=lambda x: x.submit_date)
> t.append((sorted_part[-1].submit_date, key, sorted_part))
I think you mean
for key, item_iterator in itertools.groupby(l, key=lambda x:
(x.content_type)): # Group by content type only
sorted_part = list(item_iterator) # No need to sort again
t.append((sorted_part[-1].submit_date, key, sorted_part))
Kent
More information about the Tutor
mailing list