1

I am trying to use the new multisort filter to sort an array in reverse order using a date key, but can't seem to figure out the correct syntax. Here is what I have:

{% for rides in allRides|multisort(eventDate,SORT_DESC,SORT_REGULAR) %}

This throws an error saying "Variable eventDate does not exist". eventDate is definitely a key in the array 'allRides'

If I do this, it works fine, but gets sorted by eventDate ascending:

{% for rides in allRides|sort %}

Some guidance on using the multisort filter would be greatly appreciated!

2

For those of you reading through this post, I thought it might be useful to provide a bit of background on my question. The Craft Docs list "multisort" under Filters and it sounded like something that would work nicely for me. However, the Craft Docs do not give an example, instead directing the reader to Yii docs. Those docs confirmed it was exactly what I needed - sorting an array by a given key, descending, numeric. However, the syntax does not make sense in term of using it within a template.

Here is the answer from Brad and Andrew, which works great:

{% for rides in allRides|multisort(['eventDate'],['SORT_DESC'],['SORT_REGULAR']) %}

Here is what you will find on the Yii docs page :

public static void multisort ( &$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR )

I suppose if you are a php wizard, which, as you might have guessed, I am not, you might figure this out on your own. Having said that, it would save many of us lots of time if the Craft docs simply included the example that Brad was kind enough to provide.

1

I think this should do what you're looking for:

{% for rides in allRides|multisort(['eventDate'],['SORT_DESC'],['SORT_REGULAR']) %}
  • Just need to remove the quotes from around the 2nd & 3rd arguments {% for rides in allRides|multisort(['eventDate'],[SORT_DESC],[SORT_REGULAR]) %} – user3788089 Jul 18 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.