<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>

CC BY-SA 3.0
2015-01-24T16:55:10.777Z
There's a download link on the docs page: http://michaelrog.com/craft/supersort
Michael Rog
CC BY-SA 3.0
2015-03-28T23:07:53.063Z
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.
Johannes Lamers
CC BY-SA 3.0
2015-03-29T03:55:50.143Z
Surely — I've added a code example to my answer.
Michael Rog
CC BY-SA 3.0
2015-03-29T09:28:33.613Z
@michael Great little plugin, thanks so much for making this.
carlcs
CC BY-SA 3.0
2015-01-24T00:24:13.007Z
2017-10-30T16:37:00.677Z
2017-10-30T16:37:00.677Z
Michael Rog
<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>

CC BY-SA 3.0
2014-07-25T18:24:29.170Z
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;
  }
Keith Mancuso
CC BY-SA 3.0
2014-07-25T22:50:28.940Z
@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/) )
Victor
CC BY-SA 3.0
2014-08-01T01:23:28.183Z
Sorry you are right it's working, not sure what was going on but thanks
Keith Mancuso
CC BY-SA 3.0
2014-07-10T13:32:56.587Z
2014-07-10T13:32:56.587Z
Victor
<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>

CC BY-SA 3.0
2014-07-09T19:51:10.733Z
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
jpea
CC BY-SA 3.0
2014-07-10T00:20:42.767Z
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!
Seán O'Grady
<templating><sorting>
Sort merged array by array value in Twig?
# 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 =  %}
{% set community =  %}
{% set feed = instagram|merge(community) %}

    {% for photo in feed %}
        
        
    {% 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, 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.  
Just add this to the TwigBetterSortTwigExtension.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:
{% 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**: @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/) )  
> **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 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' — 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://michaelrog.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. Michael 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**: @michael Great little plugin, thanks so much for making this.  

---

