Answers.Body: <p>The <a href="https://github.com/TopShelfCraft/SuperSort" rel="nofollow noreferrer">SuperSort</a> Plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.</p>
  
  <p>For example, in OP's case, we could sort the combined array by the <code>created_time</code> variable like this:</p>
  
  <pre><code>...
  {% set feed = instagram|merge(community) %}
  {% set sortedFeed = feed | supersort('sortAs', '{created_time}') %}
  </code></pre>
  
  <p><em>(The plugin parses <code>{created_time}</code> &mdash; a Craft '<a href="http://buildwithcraft.com/classreference/services/TemplatesService#renderObjectTemplate-detail" rel="nofollow noreferrer">micro-template</a>' &mdash; into <code>{{object.created_time}}</code>, which provides the value by which the array is sorted.)</em></p>
  
  <p>If the property of interest happened to be different between two arrays, we could include a conditional or ternary operator in the micro-template to differentiate them:</p>
  
  <pre><code>{% set sortedFeed = feed | supersort('sortAs', '{{ object.current_time is defined ?: object.someOtherProperty }}') %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-24T16:55:10.777Z
Answers.Text: There's a download link on the docs page: http://michaelrog.com/craft/supersort
Answers.UserDisplayName: Michael Rog
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-28T23:07:53.063Z
Answers.Text: I'm trying to accomplish the same. Making some sort of feed with multidimensional data array. Like combining comments feed with entries and twitter. Michael can you give us a simple example of to use your plugin? De docs are hard to understand for me.
Answers.UserDisplayName: Johannes Lamers
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-29T03:55:50.143Z
Answers.Text: Surely — I've added a code example to my answer.
Answers.UserDisplayName: Michael Rog
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-29T09:28:33.613Z
Answers.Text: @michael Great little plugin, thanks so much for making this.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-24T00:24:13.007Z
Answers.LastActivityDate: 2017-10-30T16:37:00.677Z
Answers.LastEditDate: 2017-10-30T16:37:00.677Z
Answers.UserDisplayName: Michael Rog
Answers.Body: <p>This is only possible with a custom <a href="http://twig.sensiolabs.org/doc/advanced.html">Twig Extension</a>, since sorting a multidimensional array can't be done with Twig itself.</p>
  
  <p>I've written a plugin which extends Twigs sorting methods, which you could use as a base: <a href="https://github.com/victor-in/Craft-TwigBetterSort">Craft-TwigBetterSort</a>.  </p>
  
  <p>Just add this to the <a href="https://github.com/victor-in/Craft-TwigBetterSort/blob/master/twigbettersort/twigextensions/TwigBetterSortTwigExtension.php"><em>TwigBetterSortTwigExtension.php</em></a></p>
  
  <pre class="lang-php prettyprint-override"><code>public function twig_sort($array, $method='asort', $sort_flag='SORT_REGULAR')
  {
      // ....
      case 'custom_sort_name':
          usort($array, array('TwigBetterSortTwigExtension','custom_sort_function'));
          break;
      // ....
  }
  
  private function custom_sort_function($a, $b)
  {
      // Do stuff with your array
  }
  </code></pre>
  
  <p>and use it like this in your templates:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% array|sort('custom_sort_name') %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-25T18:24:29.170Z
Answers.Text: hmm not having any luck with this. feel like im missing something.  I have object similar to the original question and i have this...
  
  case 'dateSort':
     usort($array, array('TwigBetterSortTwigExtension','created_date_sort'));
     break;
  
  ...
  
  public function created_date_sort($a, $b) {
   if ($a.created_at == $b.created_at) {
          return 0;
      }
      return ($a.created_at < $b.created_at) ? -1 : 1;
    }
Answers.UserDisplayName: Keith Mancuso
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-25T22:50:28.940Z
Answers.Text: @KeithMancuso Just tried your code on a random array: `{% set array = { 2: '5', 4: '6', 1: '5', 3: '6', 2: '7', 4: '8' } %}` and it got re-sorted. What exactly isn't working for you? (Share bigger code using [CraftBin](http://craftb.in/) )
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-01T01:23:28.183Z
Answers.Text: Sorry you are right it's working, not sure what was going on but thanks
Answers.UserDisplayName: Keith Mancuso
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-10T13:32:56.587Z
Answers.LastActivityDate: 2014-07-10T13:32:56.587Z
Answers.UserDisplayName: Victor
Body: <p>I have two Instagram API responses that I want to merge, by the photo's <code>created_time</code>. Is there a way to do that in Twig?</p>
  
  <p>Right now I am just using this</p>
  
  <pre><code>{% set instagram = &lt;data&gt; %}
  {% set community = &lt;data&gt; %}
  {% set feed = instagram|merge(community) %}
  
      {% for photo in feed %}
          &lt;img src="{{photo.images.low_resolution.url}}" alt=""&gt;
          &lt;br /&gt;
      {% endfor %}
  </code></pre>
  
  <p>Each photo has a created_time string that I'd like to sort the array by, is that possible without creating a custom Twig extension? </p>
  
  <p>The sortByField plugin looked promising and sort of what I want, but it was directly aimed at Craft entry manipulation. Is there anything hacky that could be done?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-09T19:51:10.733Z
Comments.Text: Since the only way to sort a multidimensional array in PHP is with a usort and thus a custom function based on your array, I think you'd have to create a custom twig filter. You could use one of mine as a base. It's very simple, so pretty easy to pick apart: https://github.com/jpeacock/highestmultiple
Comments.UserDisplayName: jpea
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-10T00:20:42.767Z
Comments.Text: Yeah it looks that way! I've already written a primitive plugin for getting the first paragraph of an Rich Text Field so i should be able to hack something together. Which is a pain, because the sortByField plugin gets quite close. Thanks!
Comments.UserDisplayName: Seán O'Grady
Tags: <templating><sorting>
Title: Sort merged array by array value in Twig?
ThreadText: # Sort merged array by array value in Twig?
  
  (Asked by: username_0 on 2014-07-09)
  
  I have two Instagram API responses that I want to merge, by the photo's `created_time`. Is there a way to do that in Twig?
  
  Right now I am just using this
  
  ```
  {% set instagram = <data> %}
  {% set community = <data> %}
  {% set feed = instagram|merge(community) %}
  
      {% for photo in feed %}
          <img src="{{photo.images.low_resolution.url}}" alt="">
          <br />
      {% endfor %}
  
  ```
  
  Each photo has a created\_time string that I'd like to sort the array by, is that possible without creating a custom Twig extension?
  
  The sortByField plugin looked promising and sort of what I want, but it was directly aimed at Craft entry manipulation. Is there anything hacky that could be done?
  
  **Tags:** templating, sorting
  
  > **Comments:**
  > **username_1**: Since the only way to sort a multidimensional array in PHP is with a usort and thus a custom function based on your array, I think you'd have to create a custom twig filter. You could use one of mine as a base. It's very simple, so pretty easy to pick apart: https://github.com/jpeacock/highestmultiple  
  > **username_0**: Yeah it looks that way! I've already written a primitive plugin for getting the first paragraph of an Rich Text Field so i should be able to hack something together. Which is a pain, because the sortByField plugin gets quite close. Thanks!  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_5 on 2014-07-10. Score: 8 | Accepted answer
  
  This is only possible with a custom [Twig Extension](http://twig.sensiolabs.org/doc/advanced.html), since sorting a multidimensional array can't be done with Twig itself.
  
  I've written a plugin which extends Twigs sorting methods, which you could use as a base: [Craft-TwigBetterSort](https://github.com/username_5-in/Craft-TwigBetterSort).
  
  Just add this to the [*TwigBetterSortTwigExtension.php*](https://github.com/username_5-in/Craft-TwigBetterSort/blob/master/twigbettersort/twigextensions/TwigBetterSortTwigExtension.php)
  
  ```php
  public function twig_sort($array, $method='asort', $sort_flag='SORT_REGULAR')
  {
      // ....
      case 'custom_sort_name':
          usort($array, array('TwigBetterSortTwigExtension','custom_sort_function'));
          break;
      // ....
  }
  
  private function custom_sort_function($a, $b)
  {
      // Do stuff with your array
  }
  
  ```
  
  and use it like this in your templates:
  
  ```twig
  {% array|sort('custom_sort_name') %}
  
  ```
  
  > **Comments:**
  > **username_6**: hmm not having any luck with this. feel like im missing something.  I have object similar to the original question and i have this...  case 'dateSort':    usort($array, array('TwigBetterSortTwigExtension','created_date_sort'));    break;  ...  public function created_date_sort($a, $b) {  if ($a.created_at == $b.created_at) {         return 0;     }     return ($a.created_at < $b.created_at) ? -1 : 1;   }  
  > **username_5**: @username_6 Just tried your code on a random array: `{% set array = { 2: '5', 4: '6', 1: '5', 3: '6', 2: '7', 4: '8' } %}` and it got re-sorted. What exactly isn't working for you? (Share bigger code using [CraftBin](http://craftb.in/) )  
  > **username_6**: Sorry you are right it's working, not sure what was going on but thanks  
  
  ---
  
  ### Answer by username_2 on 2015-01-24. Score: 13
  
  The [SuperSort](https://github.com/TopShelfCraft/SuperSort) Plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.
  
  For example, in OP's case, we could sort the combined array by the `created_time` variable like this:
  
  ```
  ...
  {% set feed = instagram|merge(community) %}
  {% set sortedFeed = feed | supersort('sortAs', '{created_time}') %}
  
  ```
  
  *(The plugin parses `{created_time}` — a Craft '[micro-template](http://buildwithcraft.com/classreference/services/TemplatesService#renderObjectTemplate-detail)' — into `{{object.created_time}}`, which provides the value by which the array is sorted.)*
  
  If the property of interest happened to be different between two arrays, we could include a conditional or ternary operator in the micro-template to differentiate them:
  
  ```
  {% set sortedFeed = feed | supersort('sortAs', '{{ object.current_time is defined ?: object.someOtherProperty }}') %}
  
  ```
  
  > **Comments:**
  > **username_2**: There's a download link on the docs page: http://username_2.com/craft/supersort  
  > **username_3**: I'm trying to accomplish the same. Making some sort of feed with multidimensional data array. Like combining comments feed with entries and twitter. username_2 can you give us a simple example of to use your plugin? De docs are hard to understand for me.  
  > **username_2**: Surely — I've added a code example to my answer.  
  > **username_4**: @username_2 Great little plugin, thanks so much for making this.  
  
  ---
  
  