<p>Rather than changing the order of the actual array returned by the <code>group</code> filter, you can just assemble a new array that has the keys in the order you want:</p>

<pre><code>{# Get the grouped entries #}
{% groups = entries|group('foo') %}

{# Create an array of the group keys in the desired order #}
{% set keys = ['foo']|merge(groups|keys|without('foo')) %}

{# Now loop through the keys, rather than the groups directly #}
{% for key in keys %}

    {# Get the group with this key #}
    {% set group = groups[key] ?? [] %}

    {# Loop through the entries #}
    {% for entry in group %}
        ...
    {% endfor %}

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

<p>(Note that the <code>??</code> operator requires Craft 2.6.)</p>

CC BY-SA 3.0
2016-03-09T21:21:40.560Z
2016-03-10T00:27:26.930Z
2016-03-10T00:27:26.930Z
Brandon Kelly
<p>I am using the <a href="https://craftcms.com/docs/templating/filters#group" rel="nofollow"><code>group</code></a> filter to group like entries together. I need to be able to control which group is output first. How can I do this?</p>

<templating>
How can you choose which group to output first when using the group filter?
# How can you choose which group to output first when using the group filter?

(Asked by: username_0 on 2016-03-09)

I am using the [`group`](https://craftcms.com/docs/templating/filters#group) filter to group like entries together. I need to be able to control which group is output first. How can I do this?

**Tags:** templating


---

## 1 answers

### Answer by username_1 on 2016-03-09. Score: 4

Rather than changing the order of the actual array returned by the group filter, you can just assemble a new array that has the keys in the order you want:
{# Get the grouped entries #}
{% groups = entries|group('foo') %}

{# Create an array of the group keys in the desired order #}
{% set keys = ['foo']|merge(groups|keys|without('foo')) %}

{# Now loop through the keys, rather than the groups directly #}
{% for key in keys %}

    {# Get the group with this key #}
    {% set group = groups[key] ?? [] %}

    {# Loop through the entries #}
    {% for entry in group %}
        ...
    {% endfor %}

{% endfor %}

(Note that the ?? operator requires Craft 2.6.)


---

