Introduction

The sort filter is used to sort arrays or sequences of values in ascending order. It is useful when you need to ensure that items are displayed in a specific sequence, such as listing products by price or organizing names alphabetically.

Example of use

Example of how to use the sort filter to sort an array of numbers:

{% set numbers = [5, 3, 9, 1, 4] %}

{{ numbers|sort|json_encode }}

In this example, the sort filter sorts the numbers array in ascending order. The result will display the numbers arranged in increasing sequence. To facilitate viewing and sending the data via WhatsApp, we use json_encode together to convert the sorted array into a JSON string.

Example Output Code