How do you sort a list in Jinja2?

Nick Perkins picture Nick Perkins · Dec 24, 2009 · Viewed 74k times · Source

I am trying to do this:

 {% for movie in movie_list | sort(movie.rating) %}

But that's not right...the documentation is vague...how do you do this in Jinja2?

Answer

Steve S picture Steve S · Mar 30, 2011

As of version 2.6, Jinja2's built-in sort filter allows you to specify an attribute to sort by:

{% for movie in movie_list|sort(attribute='rating') %}

See http://jinja.pocoo.org/docs/templates/#sort