Answers.Body: <p>The group filter loops through the passed elements in the order that they are returned by your query and builds the groups array in the order that it encounters the values being grouped. So there’s no predictable way to control what order the array keys are ordered. </p>
  
  <p>If you want to loop through all entries by dealStatus in a fixed order you could replace your <code>for</code> loop with:</p>
  
  <pre><code>{% for status in [‘InMarket’,’postBid’,’closing’,’closed’] %}
      {% if allDealsByStatus[status] is defined %}
  
          {% set entriesInStatus = allDealsByStatus[status] %}
  
          {# your list output code here #}
  
      {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-08T19:17:42.380Z
Answers.LastActivityDate: 2018-04-08T19:17:42.380Z
Answers.UserDisplayName: Steve Rowling
Body: <p>I have a channel of entries that are being pulled into a single. I then  want to group the entries by a dropdown field called dealStatus.</p>
  
  <p>This is working correctly, however the grouping is not outputting the order correctly. I expect the order of the groups to be in the same order that the dropdown field has them.</p>
  
  <p>InMarket
  postBid
  closing
  closed</p>
  
  <p>Instead the groups are getting output in this order</p>
  
  <p>postBid
  inMarket
  closing
  closed</p>
  
  <p>How do I get the groups ordered the same as the dropdown field option order?</p>
  
  <p>My current code:</p>
  
  <pre><code>{% set allDeals = craft.entries.section('deals').relatedTo(entry) %}
  {% set allDealsByStatus = allDeals|group('dealStatus') %}
  
  {% for status, entriesInStatus in allDealsByStatus %}
      &lt;li class="accordion-item {% if loop.index =="1" %}is-active{% endif %}" data-accordion-item&gt;
        &lt;!-- Accordion tab title --&gt;
        &lt;a href="#" class="accordion-title"&gt;
        {{ status }}
        &lt;i class="arrow right"&gt;&lt;/i&gt;&lt;!-- /.arrow right --&gt;
        &lt;/a&gt;
  
        &lt;!-- Accordion tab content: it would start in the open state due to using the `is-active` state class. --&gt;
        &lt;div class="accordion-content" data-tab-content&gt;
          &lt;ul class="no-bullet market-content"&gt;
            {% for entry in entriesInStatus %}
              &lt;li&gt;
                &lt;a href=""&gt;
                  &lt;img src="http://via.placeholder.com/120x60/eaeaea?text=logo here" alt="" class="market-logo"&gt;
  
                  &lt;p&gt;{{ entry.title }}&lt;/p&gt;
  
                  &lt;img src="/assets/img/next.svg" alt="" class="go-icon"&gt;
                &lt;/a&gt;
              &lt;/li&gt;
            {% endfor %}
          &lt;/ul&gt;&lt;!-- /.no-bullet --&gt;
        &lt;/div&gt;
      &lt;/li&gt;
  {% endfor %}
  </code></pre>
  
  <p>This is on Craft 3.0.1</p>
  
Tags: <templating><craft3><group>
Title: Grouping by select - keep order of options
ThreadText: # Grouping by select - keep order of options
  
  (Asked by: username_0 on 2018-04-06)
  
  I have a channel of entries that are being pulled into a single. I then want to group the entries by a dropdown field called dealStatus.
  
  This is working correctly, however the grouping is not outputting the order correctly. I expect the order of the groups to be in the same order that the dropdown field has them.
  
  InMarket
  postBid
  closing
  closed
  
  Instead the groups are getting output in this order
  
  postBid
  inMarket
  closing
  closed
  
  How do I get the groups ordered the same as the dropdown field option order?
  
  My current code:
  
  ```
  {% set allDeals = craft.entries.section('deals').relatedTo(entry) %}
  {% set allDealsByStatus = allDeals|group('dealStatus') %}
  
  {% for status, entriesInStatus in allDealsByStatus %}
      <li class="accordion-item {% if loop.index =="1" %}is-active{% endif %}" data-accordion-item>
        <!-- Accordion tab title -->
        <a href="#" class="accordion-title">
        {{ status }}
        <i class="arrow right"></i><!-- /.arrow right -->
        </a>
  
        <!-- Accordion tab content: it would start in the open state due to using the `is-active` state class. -->
        <div class="accordion-content" data-tab-content>
          <ul class="no-bullet market-content">
            {% for entry in entriesInStatus %}
              <li>
                <a href="">
                  <img src="http://via.placeholder.com/120x60/eaeaea?text=logo here" alt="" class="market-logo">
  
                  <p>{{ entry.title }}</p>
  
                  <img src="/assets/img/next.svg" alt="" class="go-icon">
                </a>
              </li>
            {% endfor %}
          </ul><!-- /.no-bullet -->
        </div>
      </li>
  {% endfor %}
  
  ```
  
  This is on Craft 3.0.1
  
  **Tags:** templating, craft3, group
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-08. Score: 1 | Accepted answer
  
  The group filter loops through the passed elements in the order that they are returned by your query and builds the groups array in the order that it encounters the values being grouped. So there’s no predictable way to control what order the array keys are ordered.
  
  If you want to loop through all entries by dealStatus in a fixed order you could replace your `for` loop with:
  
  ```
  {% for status in [‘InMarket’,’postBid’,’closing’,’closed’] %}
      {% if allDealsByStatus[status] is defined %}
  
          {% set entriesInStatus = allDealsByStatus[status] %}
  
          {# your list output code here #}
  
      {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  