<p>Not really an answer that fixes the question per sé if anyone else has the same issue, but none the less the answer that worked for me was simple:</p>

<p><strong>Split up your sorting where possible and simplify things!</strong> </p>

<p>I wanted my main array to be sorted by condition A, grouped by condition B and than sort the sub arrays alphabeticaly. All done to the same array in a few lines of code so I could use this one array in my template to get all information from it. Bad idea.</p>

<p>So what I did was sort everything by condition A in PhP in my plugin, in my template I than group everything by condition B and only when I needed to iterate over my array to reveal my sub arrays, only then I sort the values alphabetically.</p>

<p>So short answer: <strong>keep it simple damnit!</strong> :)</p>

CC BY-SA 3.0
2015-12-14T11:08:13.257Z
2015-12-14T11:08:13.257Z
Tom De Smet
<p>It's a bit hard to help as you didn't provide any code. But I suggest to apply the <code>supersort</code> filter before grouping by the department property.</p>

<pre><code>{% set usersSorted = users|supersort('sortAs', '{{ object.lastname }}') %}
{% set usersGrouped = usersSorted|group('department') %}
</code></pre>

CC BY-SA 3.0
2015-12-14T10:53:48.977Z
Alright, got it working. I had to re-arrange some sorting. I tried to sort a multi-dimensional array in a very complex way and by splitting things up and sorting them when needed, I got things working.

Sorting before grouping was not really an option though. Thanks anyway for helping!
Tom De Smet
CC BY-SA 3.0
2015-12-07T21:43:51.310Z
2015-12-07T21:43:51.310Z
carlcs
<p>My problem is that I don't know/understand how to sort complex arrays.</p>

<p>I have a multi dimensional array of users from an external API. So it's not an array that I fetch from the Craft database. Let's call this array the "Main Array". </p>

<p>The Main Array contains multiple users, every user has multiple fields such as firstname, lastname, department, and so on. So every user in the Main Array is actually another array that we shall call a User Array. So the Main Array consists of multiple User arrays which consists of multiple fields.</p>

<p>First I group the Main array by the department field of a user (using the Twig group filter). So far so good, that way I can list all my users in department blocks.</p>

<p>But within those blocks, I want them to be alphabetically sorted by their lastname. So I need a way to let Craft know to sort my User Arrays by the lastname field of a user within the Main Array.</p>

<p>Seems logical and simple, but I can't figure out how. I tried SuperSort, but the standard asort function won't work. I than tried superSort('SortAs', '{lastnameField}') but that didn't work either.</p>

<sorting><array><plugin-supersort>
Sorting multi-dimensional grouped arrays
# Sorting multi-dimensional grouped arrays

(Asked by: username_0 on 2015-12-07)

My problem is that I don't know/understand how to sort complex arrays.

I have a multi dimensional array of users from an external API. So it's not an array that I fetch from the Craft database. Let's call this array the "Main Array".

The Main Array contains multiple users, every user has multiple fields such as firstname, lastname, department, and so on. So every user in the Main Array is actually another array that we shall call a User Array. So the Main Array consists of multiple User arrays which consists of multiple fields.

First I group the Main array by the department field of a user (using the Twig group filter). So far so good, that way I can list all my users in department blocks.

But within those blocks, I want them to be alphabetically sorted by their lastname. So I need a way to let Craft know to sort my User Arrays by the lastname field of a user within the Main Array.

Seems logical and simple, but I can't figure out how. I tried SuperSort, but the standard asort function won't work. I than tried superSort('SortAs', '{lastnameField}') but that didn't work either.

**Tags:** sorting, array, plugin-supersort

---

## 2 answers

### Answer by username_0 on 2015-12-14. Score: 2 | Accepted answer

Not really an answer that fixes the question per sé if anyone else has the same issue, but none the less the answer that worked for me was simple:

**Split up your sorting where possible and simplify things!**

I wanted my main array to be sorted by condition A, grouped by condition B and than sort the sub arrays alphabeticaly. All done to the same array in a few lines of code so I could use this one array in my template to get all information from it. Bad idea.

So what I did was sort everything by condition A in PhP in my plugin, in my template I than group everything by condition B and only when I needed to iterate over my array to reveal my sub arrays, only then I sort the values alphabetically.

So short answer: **keep it simple damnit!** :)


---

### Answer by username_1 on 2015-12-07. Score: 1

It's a bit hard to help as you didn't provide any code. But I suggest to apply the supersort filter before grouping by the department property.
{% set usersSorted = users|supersort('sortAs', '{{ object.lastname }}') %}
{% set usersGrouped = usersSorted|group('department') %}

> **Comments:**
> **username_0**: Alright, got it working. I had to re-arrange some sorting. I tried to sort a multi-dimensional array in a very complex way and by splitting things up and sorting them when needed, I got things working.  Sorting before grouping was not really an option though. Thanks anyway for helping!  

---

