<p>You are getting this error because the "entries" fields are always considered to be a query (ElementCriteriaModel) that can return more than one element. Therefore, if you know for sure that you will always have one and only one town linked to a business, you could do something like that:</p>

<pre><code>{% set businessesByTown = businesses|group('bizTown[0].title') %}
</code></pre>

<p>If the title field is the name of the town you should get an associative array with the town names as keys and the businesses entries as values.</p>

<p><em>Additional info about sorting, following swthate's comment below:</em></p>

<p>If you want to sort the businesses, I think it can be done in the original query, so it would become:</p>

<pre><code>{% set businesses = craft.entries.relatedTo(category).order('title') %}
</code></pre>

<p>However, the towns cannot be sorted directly since Twig's sort filter uses asort which sorts the array based on values. What you can do then is extract the <code>businessesByTown</code> array's keys in another array:</p>

<pre><code>{% set towns = businessesByTown|keys|sort %}
</code></pre>

<p>You now have a sorted list of towns. Using this list, you can loop on all towns and businesses like that:</p>

<pre><code>{% for town in towns %}
    {{ town }}
    {% set townBusinesses = businessesByTown[town] %}
    {% for business in townBusinesses %}
        {{ business.title }}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-06-16T14:55:57.360Z
Awesome, it works great. Thanks for the clear description!
Steven Thate
CC BY-SA 3.0
2017-06-16T15:10:29.560Z
One quick follow-up, if I wanted to order both businesses and towns alphabetically, where would I put `.order('title')`?
Steven Thate
CC BY-SA 3.0
2017-06-16T15:29:55.277Z
Updated my answer following your comment.
kant312
CC BY-SA 3.0
2017-06-16T07:54:43.237Z
2017-06-16T15:29:38.947Z
2017-06-16T15:29:38.947Z
kant312
<p>I have a section for Businesses. I also have categories that these businesses can belong to. On each category page I want to list all the businesses that belong to that category. So far so good, but here's the rub: Each business also has an entry field that relates the business to a Town entry.</p>

<p>Businesses belong both to a category and a town entry.</p>

<p>On the category page I want to list the businesses by towns. So instead of having a laundry list of businesses, the businesses would be broken up by the different towns they belong to:</p>

<pre><code>Town A
    - Business 1
    - Business 2
Town B
    - Business 3
Town C
    - Business 4
    - Business 5
    - Business 6
</code></pre>

<p>I am grabbing my businesses like this:</p>

<p><code>{% set businesses = craft.entries.relatedTo(category) %}</code></p>

<p>I have seen other answers to different questions suggest GROUP, but I'm not sure how to correctly go about using the group method on an array when I am trying to group via another array (entry field).</p>

<p>I tried this, but it results in "Object of class Craft\ElementCriteriaModel could not be converted to string":</p>

<p><code>{% set businessesByTown = businesses|group('bizTown') %}</code></p>

<p>I know that's fishy and incorrect, but I don't know how to accomplish it the right way. <code>bizTown</code> is the entry field I want to sort by.</p>

<p>If anyone could help, that'd be wonderful! Thank you!</p>

<entries><categories><sorting><group>
Sort entries in a category via entry field
# Sort entries in a category via entry field

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

I have a section for Businesses. I also have categories that these businesses can belong to. On each category page I want to list all the businesses that belong to that category. So far so good, but here's the rub: Each business also has an entry field that relates the business to a Town entry.
Businesses belong both to a category and a town entry.
On the category page I want to list the businesses by towns. So instead of having a laundry list of businesses, the businesses would be broken up by the different towns they belong to:
Town A
    - Business 1
    - Business 2
Town B
    - Business 3
Town C
    - Business 4
    - Business 5
    - Business 6

I am grabbing my businesses like this:
{% set businesses = craft.entries.relatedTo(category) %}
I have seen other answers to different questions suggest GROUP, but I'm not sure how to correctly go about using the group method on an array when I am trying to group via another array (entry field).
I tried this, but it results in "Object of class Craft\ElementCriteriaModel could not be converted to string":
{% set businessesByTown = businesses|group('bizTown') %}
I know that's fishy and incorrect, but I don't know how to accomplish it the right way. bizTown is the entry field I want to sort by.
If anyone could help, that'd be wonderful! Thank you!

**Tags:** entries, categories, sorting, group

---

## 1 answers

### Answer by username_1 on 2017-06-16. Score: 3 | Accepted answer

You are getting this error because the "entries" fields are always considered to be a query (ElementCriteriaModel) that can return more than one element. Therefore, if you know for sure that you will always have one and only one town linked to a business, you could do something like that:
{% set businessesByTown = businesses|group('bizTown[0].title') %}

If the title field is the name of the town you should get an associative array with the town names as keys and the businesses entries as values.
Additional info about sorting, following swthate's comment below:
If you want to sort the businesses, I think it can be done in the original query, so it would become:
{% set businesses = craft.entries.relatedTo(category).order('title') %}

However, the towns cannot be sorted directly since Twig's sort filter uses asort which sorts the array based on values. What you can do then is extract the businessesByTown array's keys in another array:
{% set towns = businessesByTown|keys|sort %}

You now have a sorted list of towns. Using this list, you can loop on all towns and businesses like that:
{% for town in towns %}
    {{ town }}
    {% set townBusinesses = businessesByTown[town] %}
    {% for business in townBusinesses %}
        {{ business.title }}
    {% endfor %}
{% endfor %}

> **Comments:**
> **username_0**: Awesome, it works great. Thanks for the clear description!  
> **username_0**: One quick follow-up, if I wanted to order both businesses and towns alphabetically, where would I put `.order('title')`?  
> **username_1**: Updated my answer following your comment.  

---

