From 205bae550353e47d5e7a84a0766b47a572b34194 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 17 Sep 2008 13:57:45 +0200 Subject: [PATCH] Implemented a sort filter that sorts arbitrary sequences. --HG-- branch : trunk --- CHANGES | 2 ++ jinja2/filters.py | 30 +++++++++++++++++++++++++----- tests/test_filters.py | 5 +++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 14ee198..079d2fc 100644 --- a/CHANGES +++ b/CHANGES @@ -26,6 +26,8 @@ Version 2.1 executed under some conditions even though they were not part of a visible area. +- added `sort` filter that works like `dictsort` but for arbitrary sequences. + Version 2.0 ----------- (codename jinjavitus, released on July 17th 2008) diff --git a/jinja2/filters.py b/jinja2/filters.py index da5f99d..78d4cb6 100644 --- a/jinja2/filters.py +++ b/jinja2/filters.py @@ -137,7 +137,7 @@ def do_title(s): def do_dictsort(value, case_sensitive=False, by='key'): - """ Sort a dict and yield (key, value) pairs. Because python dicts are + """Sort a dict and yield (key, value) pairs. Because python dicts are unsorted you may want to use this function to order them by either key or value: @@ -162,15 +162,34 @@ def do_dictsort(value, case_sensitive=False, by='key'): '"key" or "value"') def sort_func(item): value = item[pos] - if isinstance(value, basestring): - value = unicode(value) - if not case_sensitive: - value = value.lower() + if isinstance(value, basestring) and not case_sensitive: + value = value.lower() return value return sorted(value.items(), key=sort_func) +def do_sort(value, case_sensitive=False): + """Sort an iterable. If the iterable is made of strings the second + parameter can be used to control the case sensitiveness of the + comparison which is disabled by default. + + .. sourcecode:: jinja + + {% for item in iterable|sort %} + ... + {% endfor %} + """ + if not case_sensitive: + def sort_func(item): + if isinstance(item, basestring): + item = item.lower() + return item + else: + sort_func = None + return sorted(seq, key=sort_func) + + def do_default(value, default_value=u'', boolean=False): """If the value is undefined it will return the passed default value, otherwise the value of the variable: @@ -653,6 +672,7 @@ FILTERS = { 'join': do_join, 'count': len, 'dictsort': do_dictsort, + 'sort': do_sort, 'length': len, 'reverse': do_reverse, 'center': do_center, diff --git a/tests/test_filters.py b/tests/test_filters.py index b2d4340..f70bb4c 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -67,6 +67,7 @@ GROUPBY = '''{% for grouper, list in [{'foo': 1, 'bar': 2}, {{ grouper }}: {{ list|join(', ') }} {% endfor %}''' FILTERTAG = '''{% filter upper|replace('FOO', 'foo') %}foobar{% endfilter %}''' +SORT = '''{{ ['foo', 'Bar', 'blah']|sort }}''' def test_capitalize(env): @@ -324,3 +325,7 @@ def test_safe(): assert tmpl.render() == '
foo
' tmpl = env.from_string('{{ "
foo
" }}') assert tmpl.render() == '<div>foo</div>' + + +def test_sort(env): + assert env.from_string(SORT).render() == "['Bar', 'blah', 'foo']" -- 2.26.2