<p>It’s not totally clear from your question where you’re building the relation to the categories,
but I have a feeling that this is the solution you’re looking for.</p>

<pre><code>{% set groupParam = '{ object.categoriesFieldHandle.first() ? object.categoriesFieldHandle.first().title : "No categories" }' %}
{% for groupTitle, entriesInGroup in entry.entriesFieldHandle|group(groupParam) %}
    {{ groupTitle }}

    {% for entry in entriesInGroup %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-07-06T15:53:16.873Z
I am trying to show the entries selected in the entries field but sort them in category groups. The entries I'm pulling from have a category field called 'specialityCat'. Would I need to use this in conjunction with the 2 'set' parameters I have above? Not getting this to work yet but I have this: 

`{% for group, entriesInGroup in entry.physicians|group('specialityCat') %}
    {{ group.title }}

    {% for entry in entriesInGroup %}
        <p>{{ entry.title }}</p>
    {% endfor %}
{% endfor %}`
glider
CC BY-SA 3.0
2017-07-06T21:03:57.853Z
@David I updated the code example, should work now.
carlcs
CC BY-SA 3.0
2017-07-06T14:03:46.567Z
2017-07-07T14:01:58.027Z
2017-07-07T14:01:58.027Z
carlcs
<p>I have an entries field 'physicians'. I want to output the selections from that field and sort them by category (also named 'physicians') - with the category heading above each category group. </p>

<p>I can't figure out if I should just output the category list and filter the result by the field or if I should output the entries field and then sort them by category. I'm trying the latter right now but not sure of how to get the categories to work. </p>

<pre><code>{% set phyGroup = craft.categories.group('physicians') %}
{% set phyList = craft.entries.relatedTo('physicians').order('lastName asc') %}

{% for physicians in phyGroup %}
   &lt;h3&gt;{{ category }}&lt;/h3&gt;
   {% for entry in phyList %}
      {{ entry.lastName }} ...
   {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-07-06T13:59:31.177Z
Am I right in assuming that you have a Categories field added to the field layout of the entries that you pick with your `physicians` Entries field? What’s the handle of that field?
carlcs
CC BY-SA 3.0
2017-07-06T15:27:43.700Z
Yes, on the entries that I pick from there is a categories field named 'specialityCat'.
glider
<categories><entries-field>
Sorting Entries field results by category
# Sorting Entries field results by category

(Asked by: username_0 on 2017-06-30)

I have an entries field 'physicians'. I want to output the selections from that field and sort them by category (also named 'physicians') - with the category heading above each category group. 
I can't figure out if I should just output the category list and filter the result by the field or if I should output the entries field and then sort them by category. I'm trying the latter right now but not sure of how to get the categories to work. 
{% set phyGroup = craft.categories.group('physicians') %}
{% set phyList = craft.entries.relatedTo('physicians').order('lastName asc') %}

{% for physicians in phyGroup %}
   {{ category }}
   {% for entry in phyList %}
      {{ entry.lastName }} ...
   {% endfor %}
{% endfor %}

**Tags:** categories, entries-field
> **Comments:**
> **username_1**: Am I right in assuming that you have a Categories field added to the field layout of the entries that you pick with your `physicians` Entries field? What’s the handle of that field?  
> **username_0**: Yes, on the entries that I pick from there is a categories field named 'specialityCat'.  

---

## 1 answers

### Answer by username_1 on 2017-07-06. Score: 5 | Accepted answer

It’s not totally clear from your question where you’re building the relation to the categories,
but I have a feeling that this is the solution you’re looking for.
{% set groupParam = '{ object.categoriesFieldHandle.first() ? object.categoriesFieldHandle.first().title : "No categories" }' %}
{% for groupTitle, entriesInGroup in entry.entriesFieldHandle|group(groupParam) %}
    {{ groupTitle }}

    {% for entry in entriesInGroup %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}

> **Comments:**
> **username_0**: I am trying to show the entries selected in the entries field but sort them in category groups. The entries I'm pulling from have a category field called 'specialityCat'. Would I need to use this in conjunction with the 2 'set' parameters I have above? Not getting this to work yet but I have this:   `{% for group, entriesInGroup in entry.physicians|group('specialityCat') %}     {{ group.title }}      {% for entry in entriesInGroup %}         <p>{{ entry.title }}</p>     {% endfor %} {% endfor %}`  
> **username_1**: @David I updated the code example, should work now.  

---

