From: Armin Ronacher Date: Tue, 10 Jul 2007 18:07:15 +0000 (+0200) Subject: [svn] updated groupby filter for better sorting X-Git-Tag: 2.0rc1~283 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=fecf882baeb9fa2adb2d8e4fe44ea3b7ffd0ffdb;p=jinja2.git [svn] updated groupby filter for better sorting --HG-- branch : trunk --- 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