njk

Sort array by attribute

Per official .njk documentation:

sort(arr, reverse, caseSens, attr)

Sort arr with JavaScript's arr.sort function. If reverse is true, result will be reversed. Sort is case-insensitive by default, but setting caseSens to true makes it case-sensitive. If attr is passed, will compare attr from each item.

But you can actually do this trick:


{% for item in array | sort(attribute='weight') %}

...

{% endfor %}