[Tutor] sorting objects on two attributes

Andreas Kostyrka andreas at kostyrka.org
Mon Mar 3 15:42:43 CET 2008

Well, this assumes that all named attributes do exist. If not, you need
to replace x.attr with getattr(x, "attr", defaultvalue) ;)

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))


t = sum([x[2] for x in t], [])

Totally untested, as written in the MTA :)


Am Montag, den 03.03.2008, 22:19 +0800 schrieb Eric Abrahamsen:
> I have a grisly little sorting problem to which I've hacked together a  
> solution, but I'm hoping someone here might have a better suggestion.
> I have a list of objects, each of which has two attributes, object_id  
> and submit_date. What I want is to sort them by content_type, then by  
> submit_date within content_type, and then sort each content_type block  
> according to which block has the newest object by submit_date. (This  
> sequence of sorting might not be optimal, I'm not sure). I'm actually  
> creating a list of recent comments on blog entries for a python-based  
> web framework, and want to arrange the comments according to blog  
> entry (content_type), by submit_date within that entry, with the  
> entries with the newest comments showing up on top.
> I don't believe a single cmp function fed to list.sort() can do this,  
> because you can't know how two objects should be compared until you  
> know all the values for all the objects. I'd be happy to be proven  
> wrong here.
> After some false starts with dictionaries, here's what I've got.  
> Queryset is the original list of comments (I'm doing this in django),  
> and it returns a list of lists, though I might flatten it afterwards.  
> It works, but it's ghastly unreadable and if there were a more  
> graceful solution I'd feel better about life in general:
> def make_com_list(queryset):
>     ids = set([com.object_id for com in queryset])
>     xlist = [[com for com in queryset if com.object_id == i] for i in  
> ids]
>     for ls in xlist:
>         ls.sort(key=lambda x: x.submit_date)
>     xlist.sort(key=lambda x: max([com.submit_date for com in x]),  
> reverse=True)
>     return xlist
> I'd appreciate any hints!
> Thanks,
> Eric
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20080303/7ca5901e/attachment.pgp 

More information about the Tutor mailing list