<p>Using the <code>group</code> filter you will always end up with the sort order of your entries. So you would need to sort the entries by your categories field, but this probably doesn't work.</p>

<p>But you can do the following:</p>

<pre><code>{# Get the categories related to my "entrySection" entries in custom order #}
{% set entries = craft.entries.section('entrySection') %}
{% set relatedCats = craft.categories.relatedTo(entries).order('customCatField desc') %}

{# Loop the categories #}
{% for category in relatedCats %}

    {# Get and loop through the 6 most recent entries related to this cat #}
    {% set relatedEntries = craft.entries.relatedTo(category).limit('6') %}
    {% for entry in relatedEntries %}
        ...
    {% endfor %}

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

CC BY-SA 3.0
2014-12-04T17:52:16.190Z
For whatever reason, putting a category field into the order function threw errors, but I was able to get it working by using dateUpdated instead. Otherwise this worked perfectly!
brandongcarroll
CC BY-SA 3.0
2014-12-04T18:34:33.937Z
With "customCatField" I actually meant a custom field assigned to your categories (e.g. category color)! Glad it worked for you :)
carlcs
CC BY-SA 3.0
2014-12-08T14:03:24.807Z
Thank you @carlcs I had looked at that too, but yes that does exactly what I need after actually implementing it :)
Gareth
CC BY-SA 3.0
2014-12-04T16:13:04.677Z
2014-12-04T16:13:04.677Z
carlcs
<p>This is what I'm trying to accomplish:</p>

<ul>
<li>All categories displayed on one page, with entries returned from each.</li>
<li>Only six entries shown in each category.</li>
<li>Categories need to be in a specific order.</li>
</ul>

<p>I've got the first two points working, but can't figure out how to change the order of the categories. Am I going about this the wrong way?</p>

<pre><code>{% set entries = craft.entries.section('entrySection').find() %}

{% for category, catEntries in entries | group('categoryField.first().title') %}

    {% for entry in catEntries|slice(0,6) %}

    {% endfor %}

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

<categories><forloop><order>
How to order categories in key/value for loop?
# How to order categories in key/value for loop?

(Asked by: username_0 on 2014-12-04)

This is what I'm trying to accomplish:

All categories displayed on one page, with entries returned from each.
Only six entries shown in each category.
Categories need to be in a specific order.

I've got the first two points working, but can't figure out how to change the order of the categories. Am I going about this the wrong way?
{% set entries = craft.entries.section('entrySection').find() %}

{% for category, catEntries in entries | group('categoryField.first().title') %}

    {% for entry in catEntries|slice(0,6) %}

    {% endfor %}

{% endfor %}

**Tags:** categories, forloop, order


---

## 1 answers

### Answer by username_1 on 2014-12-04. Score: 2

Using the group filter you will always end up with the sort order of your entries. So you would need to sort the entries by your categories field, but this probably doesn't work.
But you can do the following:
{# Get the categories related to my "entrySection" entries in custom order #}
{% set entries = craft.entries.section('entrySection') %}
{% set relatedCats = craft.categories.relatedTo(entries).order('customCatField desc') %}

{# Loop the categories #}
{% for category in relatedCats %}

    {# Get and loop through the 6 most recent entries related to this cat #}
    {% set relatedEntries = craft.entries.relatedTo(category).limit('6') %}
    {% for entry in relatedEntries %}
        ...
    {% endfor %}

{% endfor %}

> **Comments:**
> **username_0**: For whatever reason, putting a category field into the order function threw errors, but I was able to get it working by using dateUpdated instead. Otherwise this worked perfectly!  
> **username_1**: With "customCatField" I actually meant a custom field assigned to your categories (e.g. category color)! Glad it worked for you :)  
> **username_2**: Thank you @username_1 I had looked at that too, but yes that does exactly what I need after actually implementing it :)  

---

