Twig sort array of objects by field

后端 未结 8 2126
感动是毒
感动是毒 2020-12-17 09:31

I have entity with fields:

User

  • name
  • lastname
  • age

and few more. Im sending to Twig array with objec

8条回答
  •  感动是毒
    2020-12-17 09:49

    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) %}
        {{ user.name }} {{ user.lastname}} {{ user.age}}
    {% endfor %}
    

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

提交回复
热议问题