Sort merged array by array value in Twig?

I have entity with fields:

User --name --lastname --age

and few more. Im sending to Twig array with objects user.

In twig display users in table:

{% for user in users %}
    <td>{{ user.name }}<td> <td>{{ user.lastname}}<td> <td>{{ user.age}}<td>
{% endfor %}

now i whant to show the user from the small age to the bigger

You probably don’t want to add the sorting logic to your twig template to keep your templates maintainable.

The easiest way is to create a list of users sorted by age in your controller and pass the sorted list to Twig.

You can also look into creating a twig extension for filtering the users. If you create a sort_age filter, you can then use this filter in your Twig template to sort the users array:

{% for user in users|sort_age %}
    <td>{{ user.name }}<td> <td>{{ user.lastname}}<td> <td>{{ user.age}}<td>
{% endfor %}