Almost. And better than my original idea. You could have a hierarchical sort function:
def hiersort(a,b): if a.attr1 != b.attr1: return cmp(a.attr1, b.attr1) else: if a.attr2 != b.attr2: return cmp(a.attr2, b.attr2) else: return cmp(a.attr3, b.att3) l.sort(hiersort) You can keep nesting for more than three attributes, or you could make it arbitrary by setting it up recursively and setting the attribute hierarchy as a parameter somewhere. But that's probably unnecessarily fancy. Cheers On Monday 03 March 2008 08:42, Andreas Kostyrka wrote: > 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.sort() > > t = sum([x[2] for x in t], []) > > Totally untested, as written in the MTA :) > > Andreas > > 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@python.org > > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor