2

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) %}
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Jose Angel
  • 328
  • 2
  • 16

1 Answers1

3

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

Iskren
  • 1,301
  • 10
  • 15