The sort filter sorts an array. By default, it will sort in ascending order (A-Z), but you can pass the direction argument to have it sort in descending order (Z-A).

You would typically save the sorted array back to the original variable or to a new variable.

{% set array = array|sort %}

{% set array = array|sort('desc') %}

Instead of passing the direction argument you could simply use the rsort filter to sort in descending order.

You can use this filter within a for loop.

{% for item in items|sort %}
{% endfor %}

You can also use this filter in combination with the join filter to sort and output the array values in one step.

{{ [5,8,2,3]|sort|join(', ') }}

That would output: 2, 3, 5, 8


The sort filter has the following signature.


Argument Description

Sets the direction to sort by.

Allowed values are: "asc" or "desc".

"asc" means that the sorting will be in ascending order (A-Z).

"desc" means that the sorting will be in descending order (Z-A).

If this parameter is not passed then sorting will be done in ascending order.


{% set array = array|sort('desc') %}

< Back to the list of filters