fabpot/Twig — pull request #535 by inmarelibero (2011-11-26 19:24:54+00:00)
Title: Array sorting by object property or array index

Let's put I have an array of Author objects (id, firstname, lastname), or an array of arrays like:

[
{'firstname': 'Woody', 'lastname': 'Allen'},
{'firstname': 'Wayne', 'lastname': 'Coover'},
...
]

and I want to sort them by "lastname" object attribute or array index.

This pull request aims to improve the twig_sort_filter() function, allowing it to accept the attribute of an object or the index of an array a user wants to order an array with.
It can also sort hybrid arrays.

So then a user can use the Twig sort() function like:

{% for entity in authors | sort('firstname') %}
{{ entity.firstname }} {{ entity.lastname }}
{% endfor %}

and for case sensitive comparation:

{% for entity in authors | sort('firstname', false) %}
{{ entity.firstname }} {{ entity.lastname }}
{% endfor %}