<p>First, grab the list of your products, filtered by category:</p>

<pre><code>{% set productList = craft.entries(...).relatedTo(...) %}
</code></pre>

<p>Group all products by size using the <code>group</code> filter</p>

<pre><code>{% set productList = productList | group('sizeCategory.first.whatever')
</code></pre>

<p>Now you have an array of arrays. The keys in the array hold the various values of the size variable, and the values of the array hold the arrays of entries within those size groups. (Within the groups, any ordering parameters you set on the original ElementCriteriaModel should be retained.)</p>

<p>To sort the groups according to the array size, you can use <a href="http://michaelrog.com/craft/lagniappe/supersort" rel="nofollow">SuperSort</a>'s <code>ksort</code> functionality:</p>

<pre><code>{% set productList = productList | supersort('ksort', false, SORT_NUMERIC) %}
</code></pre>

<p>To finish, just loop through this list of lists, outputting them in whatever style/markup you want:</p>

<pre><code>{% for size, products in productList %}

    {{ size }}

    {% for product in products %}

        {{ size }}
        {{ product.whatever }}

    {% endfor %}

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

CC BY-SA 3.0
2015-04-06T21:55:07.697Z
2015-04-07T17:21:46.027Z
2015-04-07T17:21:46.027Z
Michael Rog
<p>You should be able to order them using a combination of the <code>order</code> method (if needed) and <code>group</code> twig filter.</p>

<pre><code>{% set productList = craft.entries.(params).order('title')|group('myCategoriesField.first().title') %}
</code></pre>

<p>The problem here is that the products might end up being displayed in the order that the size categories were created (i.e. postDate) and not the category 'size' title. If this is a problem then your solution may in fact be the only way. Have a look at <a href="https://craftcms.stackexchange.com/questions/7194/order-entries-by-category-field">this question</a> for alternative answer and discussion of some of the issues.</p>

CC BY-SA 3.0
2015-04-03T00:16:25.443Z
They probably end up being sorted in the order in which they get found in the title ordered entries list. So first entry "Anton" got the category "Xylophone" assigned and this makes "Xylophone the first key in the grouped "productList" array.
carlcs
CC BY-SA 3.0
2015-04-03T00:22:51.110Z
This probably could be solved by sorting the array using something like that [SuperSort](http://michaelrog.com/craft/lagniappe/supersort) plugin. `{% do productList|supersort(ksort) %}` should probably be the method one needs here.
carlcs
CC BY-SA 3.0
2015-04-06T19:39:29.680Z
Thanks for your help! I had used that question for testing out solutions before I posted my own question. I still don't quite understand why using the group twig filter didn't work for me, but the results it returned were not in the same order as the list of categories in the size category group. For example, my size categories are 4, 10, 25 320, 1280 (added to Craft in the order I want them displayed - in other words, the ids are in the correct order). The group filter returned the list as 4, 10, 1280, 320, 25...???
Chandra
CC BY-SA 3.0
2015-04-06T20:41:07.593Z
@carlcs I tried the SuperSort plugin. The product list was sorted correctly for the size category field using `{% set sortedProducts = productList | supersort('sortAs', '{sizeCategory.first}', SORT_NUMERIC) %}` but it seems to be ignoring the order parameters I set in the original params. In other words, I need it ordered by size, then by two other custom numeric fields.
Chandra
CC BY-SA 3.0
2015-04-02T23:40:59.183Z
2015-04-02T23:56:22.120Z
2017-04-13T12:51:00.303Z
Douglas McDonald
<p>I have an array of products that has already been filtered by a brand category. I want to display them in a list ordered by a size category. This is working, but I think there must be a better way:</p>

<pre><code>{% set productList = craft.entries(params) %} 
  {# where params includes a relatedTo parameter already #}

    {% for size in sizes %}
        {% for product in productList %}
            {% if product.sizeCategory.first.id == size.id %}
                {{ product.title }}&lt;br /&gt;
            {% endif %}
        {% endfor %}
    {% endfor %}
</code></pre>

<templating><categories><sorting>
Organize filtered entries by a specific category
# Organize filtered entries by a specific category

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

I have an array of products that has already been filtered by a brand category. I want to display them in a list ordered by a size category. This is working, but I think there must be a better way:
{% set productList = craft.entries(params) %} 
  {# where params includes a relatedTo parameter already #}

    {% for size in sizes %}
        {% for product in productList %}
            {% if product.sizeCategory.first.id == size.id %}
                {{ product.title }}
            {% endif %}
        {% endfor %}
    {% endfor %}

**Tags:** templating, categories, sorting


---

## 2 answers

### Answer by username_1 on 2015-04-06. Score: 2 | Accepted answer

First, grab the list of your products, filtered by category:
{% set productList = craft.entries(...).relatedTo(...) %}

Group all products by size using the group filter
{% set productList = productList | group('sizeCategory.first.whatever')

Now you have an array of arrays. The keys in the array hold the various values of the size variable, and the values of the array hold the arrays of entries within those size groups. (Within the groups, any ordering parameters you set on the original ElementCriteriaModel should be retained.)
To sort the groups according to the array size, you can use SuperSort's ksort functionality:
{% set productList = productList | supersort('ksort', false, SORT_NUMERIC) %}

To finish, just loop through this list of lists, outputting them in whatever style/markup you want:
{% for size, products in productList %}

    {{ size }}

    {% for product in products %}

        {{ size }}
        {{ product.whatever }}

    {% endfor %}

{% endfor %}


---

### Answer by username_2 on 2015-04-02. Score: 2

You should be able to order them using a combination of the order method (if needed) and group twig filter.
{% set productList = craft.entries.(params).order('title')|group('myCategoriesField.first().title') %}

The problem here is that the products might end up being displayed in the order that the size categories were created (i.e. postDate) and not the category 'size' title. If this is a problem then your solution may in fact be the only way. Have a look at this question for alternative answer and discussion of some of the issues.

> **Comments:**
> **username_3**: They probably end up being sorted in the order in which they get found in the title ordered entries list. So first entry "Anton" got the category "Xylophone" assigned and this makes "Xylophone the first key in the grouped "productList" array.  
> **username_3**: This probably could be solved by sorting the array using something like that [SuperSort](http://michaelrog.com/craft/lagniappe/supersort) plugin. `{% do productList|supersort(ksort) %}` should probably be the method one needs here.  
> **username_0**: Thanks for your help! I had used that question for testing out solutions before I posted my own question. I still don't quite understand why using the group twig filter didn't work for me, but the results it returned were not in the same order as the list of categories in the size category group. For example, my size categories are 4, 10, 25 320, 1280 (added to Craft in the order I want them displayed - in other words, the ids are in the correct order). The group filter returned the list as 4, 10, 1280, 320, 25...???  
> **username_0**: @username_3 I tried the SuperSort plugin. The product list was sorted correctly for the size category field using `{% set sortedProducts = productList | supersort('sortAs', '{sizeCategory.first}', SORT_NUMERIC) %}` but it seems to be ignoring the order parameters I set in the original params. In other words, I need it ordered by size, then by two other custom numeric fields.  

---

