From fecf882baeb9fa2adb2d8e4fe44ea3b7ffd0ffdb Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 10 Jul 2007 20:07:15 +0200 Subject: [PATCH] [svn] updated groupby filter for better sorting --HG-- branch : trunk --- jinja/filters.py | 6 ++---- jinja/utils.py | 7 +++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/jinja/filters.py b/jinja/filters.py index 6e74164..6407827 100644 --- a/jinja/filters.py +++ b/jinja/filters.py @@ -877,12 +877,10 @@ def do_groupby(attribute): """ def wrapped(env, context, value): expr = lambda x: env.get_attribute(x, attribute) - rv = [{ + return [{ 'grouper': a, 'list': list(b) - } for a, b in groupby(sorted(value, key=expr), expr)] - rv.sort() - return rv + } for a, b in sorted(groupby(sorted(value, key=expr), expr))] return wrapped diff --git a/jinja/utils.py b/jinja/utils.py index c4a2bb2..a461bd7 100644 --- a/jinja/utils.py +++ b/jinja/utils.py @@ -61,9 +61,12 @@ except NameError: try: sorted = sorted except NameError: - def sorted(seq, reverse=False): + _cmp = cmp + def sorted(seq, cmp=None, key=None, reverse=False): rv = list(seq) - rv.sort() + if key is not None: + cmp = lambda a, b: _cmp(key(a), key(b)) + rv.sort(cmp) if reverse: rv.reverse() return rv -- 2.26.2