Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T11:08:13.257Z
Answers.LastActivityDate: 2015-12-14T11:08:13.257Z
Answers.UserDisplayName: Tom De Smet
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T10:53:48.977Z
Answers.Text: 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!
Answers.UserDisplayName: Tom De Smet
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-07T21:43:51.310Z
Answers.LastActivityDate: 2015-12-07T21:43:51.310Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <sorting><array><plugin-supersort>
Title: Sorting multi-dimensional grouped arrays
ThreadText: # 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!  
  
  ---
  
  