Answers.Body: <p>I solved it by being specific about the order I want the site groups to appear - through twig filtering and array merging. Since I actually need a specific sorting, and there are no custom fields available for Site Groups, I see no other way (though I realize I can do this with a custom filter or similar):</p>
  <pre><code>{% set allGroups = craft.app.sites.getAllGroups()|filter(group =&gt; group.sites|length &gt; 0) %}
  {% set siteGroupA = allGroups|filter(group =&gt; group.id == 3) %}
  {% set siteGroupB = allGroups|filter(group =&gt; group.id == 4) %}
  {% set siteGroupC = allGroups|filter(group =&gt; group.id == 1) %}
  {% set siteGroupD = allGroups|filter(group =&gt; group.id == 2) %}
  {% set orderedGroups = siteGroupA|merge(siteGroupB)|merge(siteGroupC)|merge(siteGroupD) %}
  
  {% for siteGroup in orderedGroups %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-15T08:52:12.687Z
Answers.LastActivityDate: 2023-08-21T13:08:41.003Z
Answers.LastEditDate: 2023-08-21T13:08:41.003Z
Answers.UserDisplayName: nitech
Answers.Body: <p><code>getAllGroups()</code> isn't actually a query per-se. It returns a <a href="https://github.com/craftcms/cms/blob/develop/src/base/MemoizableArray.php" rel="nofollow noreferrer"><code>MemoizableArray</code></a> which--while it does have a couple of query-ish methods--is essentially an array that implements a memoizing caching layer.</p>
  <p>You can sort it yourself if you like; the easiest way is probably to make a <a href="https://laravel.com/docs/9.x/collections#available-methods" rel="nofollow noreferrer">Collection</a> out of it, and use <a href="https://laravel.com/docs/9.x/collections#method-sortby" rel="nofollow noreferrer"><code>sortBy()</code></a> or the like.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-15T08:49:57.027Z
Answers.LastActivityDate: 2023-08-15T08:49:57.027Z
Answers.UserDisplayName: andrew.welch
Answers.Body: <p>Site groups are automatically sorted alphabetically in the interface, and <code>craft.app.sites.getAllGroups()</code> should return them in alphabetical order as well. Beyond that, site groups don't support a manual sort order or custom fields, so you will need to find a different solution.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-15T08:41:20.783Z
Answers.LastActivityDate: 2023-08-15T08:41:20.783Z
Answers.UserDisplayName: MoritzLost
Body: <p>I have this query that lists out the site groups:</p>
  <p><code>craft.app.sites.getAllGroups()</code></p>
  <p>How can I order the result? Ideally I'd like to add a custom field to the Site Groups entry, but it doesn't seem Craft supports that.</p>
  
Tags: <order>
Title: How can I order Site Groups?
ThreadText: # How can I order Site Groups?
  
  (Asked by: username_0 on 2023-08-15)
  
  I have this query that lists out the site groups:
  
  `craft.app.sites.getAllGroups()`
  
  How can I order the result? Ideally I'd like to add a custom field to the Site Groups entry, but it doesn't seem Craft supports that.
  
  **Tags:** order
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2023-08-15. Score: 1 | Accepted answer
  
  I solved it by being specific about the order I want the site groups to appear - through twig filtering and array merging. Since I actually need a specific sorting, and there are no custom fields available for Site Groups, I see no other way (though I realize I can do this with a custom filter or similar):
  
  ```
  {% set allGroups = craft.app.sites.getAllGroups()|filter(group => group.sites|length > 0) %}
  {% set siteGroupA = allGroups|filter(group => group.id == 3) %}
  {% set siteGroupB = allGroups|filter(group => group.id == 4) %}
  {% set siteGroupC = allGroups|filter(group => group.id == 1) %}
  {% set siteGroupD = allGroups|filter(group => group.id == 2) %}
  {% set orderedGroups = siteGroupA|merge(siteGroupB)|merge(siteGroupC)|merge(siteGroupD) %}
  
  {% for siteGroup in orderedGroups %}
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2023-08-15. Score: 1
  
  `getAllGroups()` isn't actually a query per-se. It returns a [`MemoizableArray`](https://github.com/craftcms/cms/blob/develop/src/base/MemoizableArray.php) which--while it does have a couple of query-ish methods--is essentially an array that implements a memoizing caching layer.
  
  You can sort it yourself if you like; the easiest way is probably to make a [Collection](https://laravel.com/docs/9.x/collections#available-methods) out of it, and use [`sortBy()`](https://laravel.com/docs/9.x/collections#method-sortby) or the like.
  
  
  ---
  
  ### Answer by username_2 on 2023-08-15. Score: 1
  
  Site groups are automatically sorted alphabetically in the interface, and `craft.app.sites.getAllGroups()` should return them in alphabetical order as well. Beyond that, site groups don't support a manual sort order or custom fields, so you will need to find a different solution.
  
  
  ---
  
  