<p>You could wrap this in another for loop and put a condition on the inner fors, </p>

<p>something like</p>

<pre><code>{% for option in myPositionDropdown %}
  &lt;h2&gt;{{ option.value }}&lt;/h2&gt;

  {% for position, playersInPosition in playersGrouped if position == option.value %}    
    &lt;ul&gt;

    {% for player in playersInPosition %}
      {% set playerData = craft.entries.section('myPlayersChannel').relatedTo(player).first() %}
      &lt;li&gt;
        {{ playerData.fullName }}
        {{ playerData.age }}
        ...
      &lt;/li&gt;
    {% endfor %}

    &lt;/ul&gt;
  {% endfor %}

{% endfor %}
</code></pre>

<p>(untested code)</p>

<p>That sort of idea though, loop the dropdown in order and show the grouped results. Heck, you probably don't need to group them at all since you're filtering them out in each loop.</p>

<p>HTH</p>

CC BY-SA 4.0
2019-09-27T08:49:54.960Z
2019-09-27T08:49:54.960Z
billythekid
<p>Following <a href="https://craftcms.stackexchange.com/questions/9135/group-by-field-in-matrix-field">this topic</a>, I have an additional question.</p>

<p>The results are <strong>not ordered</strong> by the order of the Dropdown Options.
How can I get this done?</p>

<p><a href="https://i.sstatic.net/Zneuh.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Zneuh.png" alt="order"></a></p>

<p>Code:</p>

<pre><code>{% set players = entry.myPlayersMatrix %}
{% set playersGrouped = players|group('myPositionDropdown.label') %}

{% for position, playersInPosition in playersGrouped %}
    &lt;h2&gt;{{ position }}&lt;/h2&gt;
    &lt;ul&gt;
    {% for player in playersInPosition %}
        {% set playerData = craft.entries.section('myPlayersChannel').relatedTo(player).first() %}
        &lt;li&gt;
            {{ playerData.fullName }}
            {{ playerData.age }}
            ...
        &lt;/li&gt;
    {% endfor %}
    &lt;/ul&gt;
{% endfor %}
</code></pre>

<matrix><order><group><filtering><sort>
Order grouped results by dropdown order
# Order grouped results by dropdown order

(Asked by: username_0 on 2019-09-26)

Following this topic, I have an additional question.
The results are not ordered by the order of the Dropdown Options.
How can I get this done?

Code:
{% set players = entry.myPlayersMatrix %}
{% set playersGrouped = players|group('myPositionDropdown.label') %}

{% for position, playersInPosition in playersGrouped %}
    {{ position }}
    
    {% for player in playersInPosition %}
        {% set playerData = craft.entries.section('myPlayersChannel').relatedTo(player).first() %}
        
            {{ playerData.fullName }}
            {{ playerData.age }}
            ...
        
    {% endfor %}
    
{% endfor %}

**Tags:** matrix, order, group, filtering, sort


---

## 1 answers

### Answer by username_1 on 2019-09-27. Score: 1

You could wrap this in another for loop and put a condition on the inner fors, 
something like
{% for option in myPositionDropdown %}
  {{ option.value }}

  {% for position, playersInPosition in playersGrouped if position == option.value %}    
    

    {% for player in playersInPosition %}
      {% set playerData = craft.entries.section('myPlayersChannel').relatedTo(player).first() %}
      
        {{ playerData.fullName }}
        {{ playerData.age }}
        ...
      
    {% endfor %}

    
  {% endfor %}

{% endfor %}

(untested code)
That sort of idea though, loop the dropdown in order and show the grouped results. Heck, you probably don't need to group them at all since you're filtering them out in each loop.
HTH


---

