<p>As is mentioned in multiple places, Twig is for viewing, not manipulating data. However, this should work.</p>

<p>We create an empty array, then loop through each entry's categories and if they aren't in the array, add them. Then finish up with a sort by title</p>

<pre><code>{% set categories = [] %}
{% for entry in entries %}
  {% set entryCategories = craft.categories.group...all() %}
   {# notice the IF to combat duplicates #}
   {% for category in entryCategories if category not in categories %}
     {% set categories = categories|merge([category]) %}
   {% endif %}
{% endfor %}
{% nav category in categories|sort((a,b) =&gt; { a.title &lt;=&gt; b.title } %} {# reverse to b.title &lt;=&gt; a.title to change sort #}
...
{% endnav %}
</code></pre>

CC BY-SA 4.0
2019-12-23T15:33:19.940Z
Unfortunately your solution still produces the same list with duplicates.
Robbie Post
CC BY-SA 4.0
2020-01-10T16:08:16.187Z
@RobbiePost sorry it's been a while. I believe you may need to work with IDs when checking, rather than the objects themselves (yay programming) which should solve the issue? I think you should be able to change the IF to `category.id not in categories|map(cat=>cat.id)` if you are still having trouble with this, let me know and I can update my answer if it works
Seraphim Jester
CC BY-SA 4.0
2019-12-19T13:45:12.913Z
2019-12-19T13:45:12.913Z
Seraphim Jester
<p>I'm generating a list of all related categories of the entries listed on the page.</p>

<pre><code>{% for entry in entries %}
    {% set categories = craft.categories.group('functieGroep').level(2).relatedTo(entry).all() %}
    {% nav category in categories %}
        &lt;option value="{{ category.title }}"&gt;{{ category.title }}&lt;/option&gt;
    {% endnav %}
{% endfor %}
</code></pre>

<p>This produces the list of categories. However, I want this list sorted by category.title and only show one occurrence of similar listed categories? </p>

<craft3><categories><relatedto><sorting>
order and limit the occurrences of related categories
# order and limit the occurrences of related categories

(Asked by: username_0 on 2019-12-19)

I'm generating a list of all related categories of the entries listed on the page.
{% for entry in entries %}
    {% set categories = craft.categories.group('functieGroep').level(2).relatedTo(entry).all() %}
    {% nav category in categories %}
        {{ category.title }}
    {% endnav %}
{% endfor %}

This produces the list of categories. However, I want this list sorted by category.title and only show one occurrence of similar listed categories?

**Tags:** craft3, categories, relatedto, sorting


---

## 1 answers

### Answer by username_1 on 2019-12-19. Score: 1

As is mentioned in multiple places, Twig is for viewing, not manipulating data. However, this should work.
We create an empty array, then loop through each entry's categories and if they aren't in the array, add them. Then finish up with a sort by title
{% set categories = [] %}
{% for entry in entries %}
  {% set entryCategories = craft.categories.group...all() %}
   {# notice the IF to combat duplicates #}
   {% for category in entryCategories if category not in categories %}
     {% set categories = categories|merge([category]) %}
   {% endif %}
{% endfor %}
{% nav category in categories|sort((a,b) => { a.title <=> b.title } %} {# reverse to b.title <=> a.title to change sort #}
...
{% endnav %}

> **Comments:**
> **username_0**: Unfortunately your solution still produces the same list with duplicates.  
> **username_1**: @RobbiePost sorry it's been a while. I believe you may need to work with IDs when checking, rather than the objects themselves (yay programming) which should solve the issue? I think you should be able to change the IF to `category.id not in categories|map(cat=>cat.id)` if you are still having trouble with this, let me know and I can update my answer if it works  

---

