<p>You should be able to do this with Craft's handy <a href="http://buildwithcraft.com/docs/templating/filters#group"><code>group</code></a> filter.</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>

CC BY-SA 3.0
2015-04-10T08:19:49.857Z
Ah - never realised it was possible with matrix fields as well. Straight forward then - thanks!
mmc501
CC BY-SA 3.0
2015-04-10T06:51:56.870Z
2015-04-10T06:51:56.870Z
carlcs
<p>Is it possible to group by a field within a matrix field.
I have a channel of players which holds their details.
I have a structure for teams which includes a matrix field for a Player.</p>

<p>The Player matrix has 2 fields:</p>

<p>1 - Position (Drop down with: Goalkeeper, Defender, Midfielder)</p>

<p>2 - Entries field linked to Players Channel.</p>

<p>When accessing the matrix is it possible to Group the blocks by the Position field?</p>

<entries><matrix><group>
Group by Field in Matrix Field
# Group by Field in Matrix Field

(Asked by: username_0 on 2015-04-10)

Is it possible to group by a field within a matrix field.
I have a channel of players which holds their details.
I have a structure for teams which includes a matrix field for a Player.

The Player matrix has 2 fields:

1 - Position (Drop down with: Goalkeeper, Defender, Midfielder)

2 - Entries field linked to Players Channel.

When accessing the matrix is it possible to Group the blocks by the Position field?

**Tags:** entries, matrix, group


---

## 1 answers

### Answer by username_1 on 2015-04-10. Score: 5 | Accepted answer

You should be able to do this with Craft's handy group filter.
{% 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 %}

> **Comments:**
> **username_0**: Ah - never realised it was possible with matrix fields as well. Straight forward then - thanks!  

---

