Answers.Body: <p>As far as I'm aware, you can only order by columns in the content table. Relationships (like categories) are stored in a separate table, and are returned as arrays (or array-like objects in the case of ElementCriteriaModels) and thus not really possible to sort on.</p>
  
  <p>What you may need to do is use both 'category' and 'departmentCat' in the ElementCriteriaModel.</p>
  
  <pre><code>{% set departmentCategories = craft.categories.group('departmentCat') %}
  {% for departmentCategory in departmentCategories %}
      {% set entries = craft.entries.relatedTo(
          'and', 
          { targetElement: category }, 
          { targetElement: departmentCategory }
      ).orderBy('title) %}
      {% for entry in entries %}
          {{ entry.title }}
      {% endfor %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-16T22:41:49.177Z
Answers.LastActivityDate: 2015-02-16T22:41:49.177Z
Answers.UserDisplayName: Douglas McDonald
Answers.Body: <p>Here's a solution using the group filter.</p>
  
  <pre><code>{% set entriesGrouped = craft.entries.relatedTo(category).order('title')|group('myNewCategoriesField.first().title') %}
  
  {% for group in entriesGrouped %}
      {% for entry in group %}
  
          {# ... #}
  
      {% endfor %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T02:21:28.773Z
Answers.Text: Good solution... sounds a lot less processor intensive than doing a bunch of additional queries.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T08:39:39.037Z
Answers.Text: This does 99% of what I'm after, to nail the last percent I need to make the groups order manually from the order of the 'myNewCategoriesField'. If I remove 'order' and 'title' it changes the sorting `{% set entriesGrouped = craft.entries.relatedTo(category) | group('myNewCategoriesField.first()') %}` But instead of beeing sorted by the 'drag-order' of the categories they are sorted by the creation order of the categories. Categories seem to get their id by the order they are created not the order they are manually sorted by. Any suggestion for nailing this last detail?
Answers.UserDisplayName: Alf Vestre
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T09:57:46.323Z
Answers.Text: @AlfVestre this is not possible with this approach, I'm afraid.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-27T11:03:23.380Z
Answers.Text: I it possible to implement this solution but with category fields not required. So it would fall back on normal entry titles when no category is added to the category field
Answers.UserDisplayName: KSPR
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-28T10:23:32.393Z
Answers.Text: @KSPR probably best to open a new question for it.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-16T23:23:27.987Z
Answers.LastActivityDate: 2015-02-16T23:23:27.987Z
Answers.UserDisplayName: carlcs
Body: <p>I'm loading a set of entries related to 'category' and ordering them by the value of a Dropdown field in the entries. This work well using this code:</p>
  
  <pre><code>{% set entries = craft.entries.relatedTo(category).order('myDropdownField') %}
  </code></pre>
  
  <p>Now I need to change the Dropdown field to a Category field, but I am not able to sort my entries by this new category field by swapping the dropdown with the category field like this:</p>
  
  <pre><code>{% set entries = craft.entries.relatedTo(category).order('myNewCatField') %}
  </code></pre>
  
  <p>It gives me this error:</p>
  
  <blockquote>
    <p>CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'departmentCat' in 'order clause'.</p>
  </blockquote>
  
  <p>I also tried to use the Craft <a href="http://buildwithcraft.com/docs/templating/filters#group" rel="noreferrer">group</a> filter, but I could not make it work in my setting.</p>
  
  <p>Any suggestions are appreciated!</p>
  
Tags: <categories><ordering>
Title: Order entries by category field
ThreadText: # Order entries by category field
  
  (Asked by: username_0 on 2015-02-16)
  
  I'm loading a set of entries related to 'category' and ordering them by the value of a Dropdown field in the entries. This work well using this code:
  
  ```
  {% set entries = craft.entries.relatedTo(category).order('myDropdownField') %}
  
  ```
  
  Now I need to change the Dropdown field to a Category field, but I am not able to sort my entries by this new category field by swapping the dropdown with the category field like this:
  
  ```
  {% set entries = craft.entries.relatedTo(category).order('myNewCatField') %}
  
  ```
  
  It gives me this error:
  
  > CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'departmentCat' in 'order clause'.
  
  I also tried to use the Craft [group](http://buildwithcraft.com/docs/templating/filters#group) filter, but I could not make it work in my setting.
  
  Any suggestions are appreciated!
  
  **Tags:** categories, ordering
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-02-16. Score: 9 | Accepted answer
  
  Here's a solution using the group filter.
  
  ```
  {% set entriesGrouped = craft.entries.relatedTo(category).order('title')|group('myNewCategoriesField.first().title') %}
  
  {% for group in entriesGrouped %}
      {% for entry in group %}
  
          {# ... #}
  
      {% endfor %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_1**: Good solution... sounds a lot less processor intensive than doing a bunch of additional queries.  
  > **username_0**: This does 99% of what I'm after, to nail the last percent I need to make the groups order manually from the order of the 'myNewCategoriesField'. If I remove 'order' and 'title' it changes the sorting `{% set entriesGrouped = craft.entries.relatedTo(category) | group('myNewCategoriesField.first()') %}` But instead of beeing sorted by the 'drag-order' of the categories they are sorted by the creation order of the categories. Categories seem to get their id by the order they are created not the order they are manually sorted by. Any suggestion for nailing this last detail?  
  > **username_2**: @username_0 this is not possible with this approach, I'm afraid.  
  > **username_3**: I it possible to implement this solution but with category fields not required. So it would fall back on normal entry titles when no category is added to the category field  
  > **username_2**: @username_3 probably best to open a new question for it.  
  
  ---
  
  ### Answer by username_1 on 2015-02-16. Score: 2
  
  As far as I'm aware, you can only order by columns in the content table. Relationships (like categories) are stored in a separate table, and are returned as arrays (or array-like objects in the case of ElementCriteriaModels) and thus not really possible to sort on.
  
  What you may need to do is use both 'category' and 'departmentCat' in the ElementCriteriaModel.
  
  ```
  {% set departmentCategories = craft.categories.group('departmentCat') %}
  {% for departmentCategory in departmentCategories %}
      {% set entries = craft.entries.relatedTo(
          'and', 
          { targetElement: category }, 
          { targetElement: departmentCategory }
      ).orderBy('title) %}
      {% for entry in entries %}
          {{ entry.title }}
      {% endfor %}
  {% endfor %}
  
  ```
  
  
  ---
  
  