Skip to content

Commit

Permalink
'reverse' option for dictsort
Browse files Browse the repository at this point in the history
  • Loading branch information
dnmvisser committed Mar 9, 2017
1 parent b5eee90 commit 76481fb
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def do_title(s):
if item])


def do_dictsort(value, case_sensitive=False, by='key'):
def do_dictsort(value, case_sensitive=False, by='key', reverse=False):
"""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:
Expand All @@ -200,6 +200,9 @@ def do_dictsort(value, case_sensitive=False, by='key'):
{% for item in mydict|dictsort %}
sort the dict by key, case insensitive
{% for item in mydict|dictsort(reverse=True) %}
sort the dict by key, case insensitive, reverse order
{% for item in mydict|dictsort(true) %}
sort the dict by key, case sensitive
Expand All @@ -219,7 +222,7 @@ def sort_func(item):
value = value.lower()
return value

return sorted(value.items(), key=sort_func)
return sorted(value.items(), key=sort_func, reverse=reverse)


@environmentfilter
Expand Down

0 comments on commit 76481fb

Please sign in to comment.