<p>Is there a reason why you don’t just drag the “Others” category to the end in the CP? The sort order will be the same for the front-end with your code:</p>

<pre><code>{% set entryIds = craft.entries.section('toys').ids() %}
{% set categories = craft.categories({
    relatedTo: { sourceElement: entryIds },
    group: 'brands',
}) %}
</code></pre>

<p>I corrected your code, you never append another <code>.craft.categories</code> criteria model to a criteria model. Have a look at the docs for the allowed syntax: </p>

<p><a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">https://craftcms.com/docs/templating/elementcriteriamodel</a></p>

<p><strong>Update:</strong></p>

<p>Here’s example code that should work to list the categories alphabetically with the special “Others” category listed at the end.</p>

<pre><code>{% set entryIds = craft.entries.section('toys').ids() %}

{% set categories = craft.categories({
    relatedTo: { sourceElement: entryIds },
    group: 'brands',
    order: 'title',
}) %}

{% set otherCategory = null %}

{% for category in categories %}
    {% if category.id == 72 %}
        {% set otherCategory = category %}
    {% else %}
        &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;
    {% endif %}
{% endfor %}

{% if otherCategory %}
    &lt;a href="{{ otherCategory.url }}"&gt;{{ otherCategory.title }}&lt;/a&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-12-06T10:46:04.423Z
If I drag it to the end will the rest remain alphabetical?
Kitsune
CC BY-SA 3.0
2016-12-06T10:47:51.683Z
No, everything will be listed the same as in the CP. Do you want alphebetically sort order for the front-end and don’t wanna take care of it in the CP?
carlcs
CC BY-SA 3.0
2016-12-06T10:53:42.173Z
Actually, both as I noticed choosing a category when creating a post is sort by structure and not title (which makes it easier to find something)
Kitsune
CC BY-SA 3.0
2016-12-06T10:56:29.437Z
Yep, it’s sorted by structure but you can change it to title sort order in the CP. Anyways, I updated the answer to show you how to list alphabetically. Let me know if you’re having problems making it work for your needs.
carlcs
CC BY-SA 3.0
2016-12-06T11:03:40.423Z
@Kitsune updated the code, using a dedicated params variable is a bit cleaner.
carlcs
CC BY-SA 3.0
2016-12-06T11:08:23.170Z
An even nicer method, but it's now not showing anything and I don't have the TWIG knowhow to immediately see why not. (FYI this is for a personal site, thank you for your help so far!)
Kitsune
CC BY-SA 3.0
2016-12-06T11:12:19.660Z
Updated the code once again, it might be easier to follow and debug.
carlcs
CC BY-SA 3.0
2016-12-06T11:24:15.277Z
Yes, it works now! Thank you, and I'll study this to full understand it (new to TWIG) it's a huge improvement of my own attempt.
Kitsune
CC BY-SA 3.0
2016-12-06T10:44:26.793Z
2016-12-06T11:18:52.443Z
2016-12-06T11:18:52.443Z
carlcs
<p>I'm trying to put a single category (as long as it's populated) at the end of an category list. My code is working so far, is there an easy way to pick out the 'Others' category and append it to the list?</p>

<pre><code>{% set entryIds = craft.entries.section('toys').ids() %}
{% set categories = craft.categories.relatedTo({ sourceElement:
    entryIds }).craft.categories.group('brands').find() %}

{% for category in categories %}
    {% set image = craft.assets.relatedTo(category).first() %}

    &lt;a href="{{ category.url }}"&gt;
        {% if image|length %}
            &lt;img src="{{ image.url }}" /&gt;
        {% else %}
            &lt;img src="{{siteUrl}}assets/img/placeholder.jpg" /&gt;
        {% endif %}
    &lt;/a&gt;
{% endfor %}
</code></pre>

<p>Thanks for any help, ready to learn something new.</p>

<categories>
Sorting categories into list put single one at end of it
# Sorting categories into list put single one at end of it

(Asked by: username_0 on 2016-12-06)

I'm trying to put a single category (as long as it's populated) at the end of an category list. My code is working so far, is there an easy way to pick out the 'Others' category and append it to the list?
{% set entryIds = craft.entries.section('toys').ids() %}
{% set categories = craft.categories.relatedTo({ sourceElement:
    entryIds }).craft.categories.group('brands').find() %}

{% for category in categories %}
    {% set image = craft.assets.relatedTo(category).first() %}

    
        {% if image|length %}
            
        {% else %}
            
        {% endif %}
    
{% endfor %}

Thanks for any help, ready to learn something new.

**Tags:** categories

---

## 1 answers

### Answer by username_1 on 2016-12-06. Score: 2 | Accepted answer

Is there a reason why you don’t just drag the “Others” category to the end in the CP? The sort order will be the same for the front-end with your code:
{% set entryIds = craft.entries.section('toys').ids() %}
{% set categories = craft.categories({
    relatedTo: { sourceElement: entryIds },
    group: 'brands',
}) %}

I corrected your code, you never append another .craft.categories criteria model to a criteria model. Have a look at the docs for the allowed syntax: 
https://craftcms.com/docs/templating/elementcriteriamodel
Update:
Here’s example code that should work to list the categories alphabetically with the special “Others” category listed at the end.
{% set entryIds = craft.entries.section('toys').ids() %}

{% set categories = craft.categories({
    relatedTo: { sourceElement: entryIds },
    group: 'brands',
    order: 'title',
}) %}

{% set otherCategory = null %}

{% for category in categories %}
    {% if category.id == 72 %}
        {% set otherCategory = category %}
    {% else %}
        {{ category.title }}
    {% endif %}
{% endfor %}

{% if otherCategory %}
    {{ otherCategory.title }}
{% endif %}

> **Comments:**
> **username_0**: If I drag it to the end will the rest remain alphabetical?  
> **username_1**: No, everything will be listed the same as in the CP. Do you want alphebetically sort order for the front-end and don’t wanna take care of it in the CP?  
> **username_0**: Actually, both as I noticed choosing a category when creating a post is sort by structure and not title (which makes it easier to find something)  
> **username_1**: Yep, it’s sorted by structure but you can change it to title sort order in the CP. Anyways, I updated the answer to show you how to list alphabetically. Let me know if you’re having problems making it work for your needs.  
> **username_1**: @Kitsune updated the code, using a dedicated params variable is a bit cleaner.  
> **username_0**: An even nicer method, but it's now not showing anything and I don't have the TWIG knowhow to immediately see why not. (FYI this is for a personal site, thank you for your help so far!)  
> **username_1**: Updated the code once again, it might be easier to follow and debug.  
> **username_0**: Yes, it works now! Thank you, and I'll study this to full understand it (new to TWIG) it's a huge improvement of my own attempt.  

---

