Skip to content

Commit

Permalink
'reverse' option for dictsort
Browse files Browse the repository at this point in the history
  • Loading branch information
dnmvisser authored and davidism committed Jul 8, 2017
1 parent 07c0b87 commit 10fc8af
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions jinja2/filters.py
Expand Up @@ -203,7 +203,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 @@ -213,6 +213,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 @@ -232,7 +235,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 10fc8af

Please sign in to comment.