jmf/twig-sort

Twig extension for sorting arrays in Twig templates.

1.0.0 2024-06-05 01:00 UTC

This package is auto-updated.

Last update: 2024-12-05 02:09:26 UTC


README

Installation & Requirements

Install with Composer:

composer require jmf/twig-sort

Usage in Twig templates

sort() filter

Sorts provided array by value, losing key-value association.

{% set sorted = values|sort %}

rsort() filter

Reverse-sorts provided array by value, losing key-value association.

{% set sorted = values|rsort %}

asort() filter

Sorts provided array by value, preserving key-value association.

{% set sorted = values|asort %}

arsort() filter

Reverse-sorts provided array by value, preserving key-value association.

{% set sorted = values|arsort %}

ksort() filter

Sorts provided array by key.

{% set sorted = values|ksort %}

krsort() filter

Reverse-sorts provided array by key.

{% set sorted = values|krsort %}

psort() filter

Sorts provided array of arrays or array of objects by properties.

{% set sorted = articles|psort('title') %}
{% set sorted = articles|psort(['title', 'author.name']) %}
{% set sorted = articles|psort({'publication_date': 'desc', 'author': 'asc'}) %}