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@python.org http://mail.python.org/mailman/listinfo/tutor