I want to sort a list by length, but I'm not sure if it is possible with Jinja2:
{% for item in item_list | sort(length) %}
I want to sort a list by length, but I'm not sure if it is possible with Jinja2:
{% for item in item_list | sort(length) %}
If item
is your custom class, you can override __lt__
and you'll be able to sort directly with the sort
filter. Since Jinja 2.6, you have a length
attribute on your item, you can do so with sort(attribute='length')
. Taken from the Jinja2 sort filter documentation