sort
¶
The sort
filter sorts an array:
並べ替えフィルターは配列を並べ替えます。
1 2 3 |
{% for user in users|sort %}
...
{% endfor %}
|
Note
ノート
Internally, Twig uses the PHP asort function to maintain index association. It supports Traversable objects by transforming those to arrays.
内部的には、Twig は PHP の asort 関数を使用してインデックスの関連付けを維持します。 Traversable オブジェクトを配列に変換することでサポートします。
You can pass an arrow function to sort the array:
アロー関数を渡して配列をソートできます。
1 2 3 4 5 6 7 8 9 10 11 |
{% 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 %}
{# output in this order: Oranges, Grapes, Apples #}
|
Note the usage of the spaceship operator to simplify the comparison.
比較を簡単にするために宇宙船演算子を使用していることに注意してください。
Arguments ¶
arrow
: An arrow functionarrow: アロー関数