How do I most efficiently write a custom filter for Jinja2 that applies to an iterable like the built-in 'sort' filter, for use in a for loop in the template?
For example:
{% for item in iterable|customsort(somearg) %}
...
{% endfor %}
See http://jinja.pocoo.org/docs/api/#writing-filters for general documentation
The same way you'd write any other filter. Here's an example that should get you started:
Don't worry about efficiency until it becomes a problem. The template engine is unlikely to be the bottle-neck in any case.