Twig sort array of objects by field

Starting with Twig 2.12 (released on October 5, 2019) you can use the sort filter with an arrow function in the arrow argument.

For example, to order by name:

{% for user in users|sort((a, b) => a.name <=> b.name) %}
    <td>{{ user.name }}</td> <td>{{ user.lastname}}</td> <td>{{ user.age}}</td>
{% endfor %}

Twig docs: https://twig.symfony.com/doc/2.x/filters/sort.html

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)