Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-06T10:46:04.423Z
Answers.Text: If I drag it to the end will the rest remain alphabetical?
Answers.UserDisplayName: Kitsune
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-06T10:47:51.683Z
Answers.Text: 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?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-06T10:53:42.173Z
Answers.Text: 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)
Answers.UserDisplayName: Kitsune
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-06T10:56:29.437Z
Answers.Text: 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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-06T11:03:40.423Z
Answers.Text: @Kitsune updated the code, using a dedicated params variable is a bit cleaner.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-06T11:08:23.170Z
Answers.Text: 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!)
Answers.UserDisplayName: Kitsune
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-06T11:12:19.660Z
Answers.Text: Updated the code once again, it might be easier to follow and debug.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-06T11:24:15.277Z
Answers.Text: 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.
Answers.UserDisplayName: Kitsune
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-06T10:44:26.793Z
Answers.LastActivityDate: 2016-12-06T11:18:52.443Z
Answers.LastEditDate: 2016-12-06T11:18:52.443Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <categories>
Title: Sorting categories into list put single one at end of it
ThreadText: # 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() %}
  
      <a href="{{ category.url }}">
          {% if image|length %}
              <img src="{{ image.url }}" />
          {% else %}
              <img src="{{siteUrl}}assets/img/placeholder.jpg" />
          {% endif %}
      </a>
  {% 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 %}
          <a href="{{ category.url }}">{{ category.title }}</a>
      {% endif %}
  {% endfor %}
  
  {% if otherCategory %}
      <a href="{{ otherCategory.url }}">{{ otherCategory.title }}</a>
  {% 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**: @username_0 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.  
  
  ---
  
  