Answers.Body: <p>Use SuperSort or write your own Twig filter for it. Craft/Twig don't come with a sort filter to sort by key and replicating the functionality  (storing array keys, nested loops) is possible but kind of messy and also not performing as well.</p>
  
  <p>I can understand the desire to not be reliant on too many plugin, but the functionality you need from SuperSort is simply a wrapper for a PHP function, so you can easily swap it out for another plugin if ever necessary.</p>
  
  <p>It is also a great opportunity to start looking into plugin dev, write your first Twig extension or a simple Template Variable might just do it.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-11T07:22:41.710Z
Answers.LastActivityDate: 2017-04-11T07:37:32.910Z
Answers.LastEditDate: 2017-04-11T07:37:32.910Z
Answers.UserDisplayName: carlcs
Body: <p>I am using the code below to output a group of sections that have entries along with their section handle. How would I order the sections (ideally by structureId)? I'm not opposed to using supersort but I haven't been able to achieve the desired result.</p>
  
  <pre><code>{% set sections = craft.sections.getAllSections() %}
  {% set entries = craft.entries.section(['first', 'second', 'third']) %}
  {% set grouped = entries|group('section') %}
  
  {% for sectionHandle, sectionEntries in grouped %}
      {% set groupSection = false %}
      {% for section in sections %}
          {% if section.name == sectionHandle %}
              {% set groupSection = section %}
          {% endif %}
      {% endfor %}
  
      {% if groupSection %}
          &lt;h1&gt;{{ groupSection.name }} - {{ groupSection.handle }}&lt;/h1&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
Tags: <structure><sections><ordering>
Title: Ordering Structure Sections with Structure Handles
ThreadText: # Ordering Structure Sections with Structure Handles
  
  (Asked by: username_0 on 2017-04-07)
  
  I am using the code below to output a group of sections that have entries along with their section handle. How would I order the sections (ideally by structureId)? I'm not opposed to using supersort but I haven't been able to achieve the desired result.
  
  ```
  {% set sections = craft.sections.getAllSections() %}
  {% set entries = craft.entries.section(['first', 'second', 'third']) %}
  {% set grouped = entries|group('section') %}
  
  {% for sectionHandle, sectionEntries in grouped %}
      {% set groupSection = false %}
      {% for section in sections %}
          {% if section.name == sectionHandle %}
              {% set groupSection = section %}
          {% endif %}
      {% endfor %}
  
      {% if groupSection %}
          <h1>{{ groupSection.name }} - {{ groupSection.handle }}</h1>
      {% endif %}
  {% endfor %}
  
  ```
  
  **Tags:** structure, sections, ordering
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-04-11. Score: 1
  
  Use SuperSort or write your own Twig filter for it. Craft/Twig don't come with a sort filter to sort by key and replicating the functionality (storing array keys, nested loops) is possible but kind of messy and also not performing as well.
  
  I can understand the desire to not be reliant on too many plugin, but the functionality you need from SuperSort is simply a wrapper for a PHP function, so you can easily swap it out for another plugin if ever necessary.
  
  It is also a great opportunity to start looking into plugin dev, write your first Twig extension or a simple Template Variable might just do it.
  
  
  ---
  
  