Answers.Body: <p>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.</p>
  
  <p>Here is the answer from Brad and Andrew, which works great:</p>
  
  <pre><code>{% for rides in allRides|multisort(['eventDate'],['SORT_DESC'],['SORT_REGULAR']) %}
  </code></pre>
  
  <p>Here is what you will find on the <a href="https://www.yiiframework.com/doc/api/2.0/yii-helpers-basearrayhelper#multisort()-detail" rel="nofollow noreferrer">Yii docs page</a> :</p>
  
  <pre><code>public static void multisort ( &amp;$array, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR )
  </code></pre>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-29T16:14:45.317Z
Answers.LastActivityDate: 2018-10-29T16:14:45.317Z
Answers.UserDisplayName: Roi Agneta
Answers.Body: <p>I think this should do what you're looking for:</p>
  
  <pre><code>{% for rides in allRides|multisort(['eventDate'],['SORT_DESC'],['SORT_REGULAR']) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-18T09:44:42.400Z
Answers.Text: Just need to remove the quotes from around the 2nd & 3rd arguments ```{% for rides in allRides|multisort(['eventDate'],[SORT_DESC],[SORT_REGULAR]) %}```
Answers.UserDisplayName: dmatthams
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-27T21:38:09.597Z
Answers.LastActivityDate: 2018-10-27T21:38:09.597Z
Answers.UserDisplayName: Brad Bell
Body: <p>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:</p>
  
  <pre><code>{% for rides in allRides|multisort(eventDate,SORT_DESC,SORT_REGULAR) %}
  </code></pre>
  
  <p>This throws an error saying "Variable eventDate does not exist". eventDate is definitely a key in the array 'allRides'</p>
  
  <p>If I do this, it works fine, but gets sorted by eventDate ascending:</p>
  
  <pre><code>{% for rides in allRides|sort %}
  </code></pre>
  
  <p>Some guidance on using the multisort filter would be greatly appreciated!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-28T00:39:42.227Z
Comments.Text: Not sure if it helps, but there's an example here -> https://twitter.com/nystudio107/status/1020719464147570688
Comments.UserDisplayName: andrew.welch
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-17T13:10:35.287Z
Comments.Text: It doesn't work for me at all and I can't find any further docs on it so hit a brick wall with my scripting. I can do it easily with PHP.
Comments.UserDisplayName: cannon303
Tags: <filter><array><sort><multisort>
Title: Syntax for Multisort Filter
ThreadText: # Syntax for Multisort Filter
  
  (Asked by: username_0 on 2018-10-25)
  
  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!
  
  **Tags:** filter, array, sort, multisort
  
  > **Comments:**
  > **username_1**: Not sure if it helps, but there's an example here -> https://twitter.com/nystudio107/status/1020719464147570688  
  > **username_2**: It doesn't work for me at all and I can't find any further docs on it so hit a brick wall with my scripting. I can do it easily with PHP.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2018-10-29. Score: 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 username_3 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](https://www.yiiframework.com/doc/api/2.0/yii-helpers-basearrayhelper#multisort()-detail) :
  
  ```
  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 username_3 was kind enough to provide.
  
  
  ---
  
  ### Answer by username_3 on 2018-10-27. Score: 1
  
  I think this should do what you're looking for:
  
  ```
  {% for rides in allRides|multisort(['eventDate'],['SORT_DESC'],['SORT_REGULAR']) %}
  
  ```
  
  > **Comments:**
  > **username_4**: Just need to remove the quotes from around the 2nd & 3rd arguments ```{% for rides in allRides|multisort(['eventDate'],[SORT_DESC],[SORT_REGULAR]) %}```  
  
  ---
  
  