1``sort`` 2======== 3 4.. versionadded:: 2.12 5 6 The ``arrow`` argument was added in Twig 2.12. 7 8The ``sort`` filter sorts an array: 9 10.. code-block:: twig 11 12 {% for user in users|sort %} 13 ... 14 {% endfor %} 15 16.. note:: 17 18 Internally, Twig uses the PHP `asort`_ function to maintain index 19 association. It supports Traversable objects by transforming 20 those to arrays. 21 22You can pass an arrow function to sort the array: 23 24.. code-block:: html+twig 25 26 {% set fruits = [ 27 { name: 'Apples', quantity: 5 }, 28 { name: 'Oranges', quantity: 2 }, 29 { name: 'Grapes', quantity: 4 }, 30 ] %} 31 32 {% for fruit in fruits|sort((a, b) => a.quantity <=> b.quantity)|column('name') %} 33 {{ fruit }} 34 {% endfor %} 35 36 {# output in this order: Oranges, Grapes, Apples #} 37 38Note the usage of the `spaceship`_ operator to simplify the comparison. 39 40Arguments 41--------- 42 43* ``arrow``: An arrow function 44 45.. _`asort`: https://secure.php.net/asort 46.. _`spaceship`: https://www.php.net/manual/en/language.operators.comparison.php 47