The Complete Walkthrough of the Jinja dictsort Filter
The Jinja dictsort filter is a powerful tool for sorting dictionaries and iterating over their contents in a predictable order. Unlike lists, standard Python dictionaries don’t maintain insertion order, which can make it difficult to display data consistently in a template. The dictsort filter solves this problem by sorting the dictionary’s items and yielding them as a list of (key, value) tuples. This gives you full control over how your dictionary data is presented.
How the dictsort Filter Works
The dictsort filter takes a dictionary as input and returns a sorted list of (key, value) pairs. It offers several parameters to customize the sorting behavior:
-
case_sensitive(boolean, defaultFalse): This controls whether the sorting is case-sensitive. By default, it sorts'a'and'B'as being equal. Setting this toTruewould sort'B'before'a'.
-
by(string, default'key'): This parameter determines whether the sorting is based on the keys or the values of the dictionary. You can set it to'key'or'value'.
-
reverse(boolean, defaultFalse): If set toTrue, the sorting order will be reversed (descending instead of ascending).
The filter is designed to be used within a for loop, where you can unpack the (key, value) pairs to access each item individually.
Practical Examples
Let’s look at some examples of how to use the dictsort filter for different sorting needs.
Example 1: Sorting by Key (Default Behavior)
By default, the filter sorts the dictionary items by their keys in a case-insensitive, ascending order.nnJinja Template:
{% set my_dict = {'b_key': 200, 'A_key': 100, 'c_key': 300} %}<ul>{% for key, value in my_dict | dictsort %}<li>{{ key }}: {{ value }}</li>{% endfor %}</ul>
Rendered HTML:
<ul><li>A_key: 100</li><li>b_key: 200</li><li>c_key: 300</li></ul>
Notice how A_key is sorted before b_key because case_sensitive is False.
Example 2: Sorting by Value
You can easily change the sorting to be based on the dictionary’s values instead of its keys by setting by='value'.Jinja Template:
{% set my_dict = {'price': 100, 'stock': 50, 'rating': 95} %}<ul>{% for key, value in my_dict | dictsort(by='value') %}<li>{{ key }}: {{ value }}</li>{% endfor %}</ul>
Rendered HTML:
<ul><li>stock: 50</li><li>rating: 95</li><li>price: 100</li></ul>
The output is sorted by the numerical values in ascending order.
Example 3: Sorting in Reverse and Case-Sensitive
To sort in descending order and with case sensitivity, you can combine the reverse and case_sensitive parameters.Jinja Template:
{% set my_dict = {'apples': 1, 'Bananas': 2, 'oranges': 3} %}<p>Case-sensitive, reverse sort by key:</p><ul>{% for key, value in my_dict | dictsort(case_sensitive=True, reverse=True) %}n <li>{{ key }}: {{ value }}</li>n{% endfor %}n</ul>
Rendered HTML:
<p>Case-sensitive, reverse sort by key:</p>n<ul>n <li>oranges: 3</li>n <li>apples: 1</li>n <li>Bananas: 2</li>n</ul>
In this case, the uppercase 'B' is sorted after lowercase 'a' and 'o', which is standard ASCII sorting behavior. This is crucial when you need to maintain specific sorting logic.
Conclusion
The Jinja dictsort filter is an indispensable tool for displaying dictionary data in a controlled and predictable manner. It allows you to sort by keys or values, handle case sensitivity, and reverse the order, all with a simple and expressive syntax. This filter ensures that your templates are not only aesthetically pleasing but also reliable and consistent in their data presentation.
