collate
3.31
The collate function was added in Twig 3.31.
The collate function compares two strings in the alphabetical order of a
language. It returns a negative number, zero, or a positive number, like the
spaceship operator, so that it can be used in the arrow function of the
sort filter:
1 2 3 4 5 6
{# athlétisme, Boxe, Échecs, équitation, judo, Yoga #}
{{ ['Yoga', 'équitation', 'Boxe', 'athlétisme', 'Échecs', 'judo']|sort((a, b) => collate(a, b))|join(', ') }}
{% for city in cities|sort((a, b) => collate(a.name, b.name)) %}
{{ city.name }}
{% endfor %}
Unlike the <=> operator, which compares strings byte per byte, it does not
sort uppercase letters before lowercase ones or accented letters after z.
By default, the function uses the current locale. You can pass it explicitly, as the order depends on the language:
1 2 3 4 5
{# cs: hrad, chata, ideal #}
{{ ['ideal', 'chata', 'hrad']|sort((a, b) => collate(a, b, 'cs'))|join(', ') }}
{# en: chata, hrad, ideal #}
{{ ['ideal', 'chata', 'hrad']|sort((a, b) => collate(a, b, 'en'))|join(', ') }}
To sort in descending order, swap the arguments: collate(b, a).
To sort on several criteria, chain the comparisons with the ?: operator:
1 2 3
{% for user in users|sort((a, b) => collate(a.lastname, b.lastname) ?: collate(a.firstname, b.firstname)) %}
{{ user.lastname }} {{ user.firstname }}
{% endfor %}
Note
The collate function is part of the IntlExtension which is not
installed by default. Install it first:
1
$ composer require twig/intl-extra
Then, on Symfony projects, install the twig/extra-bundle:
1
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment:
1 2 3 4
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());