Twig sort array of objects by field

后端 未结 8 2108
感动是毒
感动是毒 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:46

    With new version twig you can sort by colonne

    {% set fruits = [
    { name: 'Apples', quantity: 5 },
    { name: 'Oranges', quantity: 2 },
    { name: 'Grapes', quantity: 4 },] %}
    {% for fruit in fruits|sort((a, b) => a.quantity <=> b.quantity)|column('name') %}
        {{ fruit }}
    {% endfor %}
    

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

提交回复
热议问题