Jose Angel
Jose Angel

Reputation: 328

Is it possible to sort a list by length in Jinja2?

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) %}

Upvotes: 2

Views: 1162

Answers (1)

Iskren
Iskren

Reputation: 1341

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

Upvotes: 3

Related Questions