<p>Okay, I've been able to solve this and figured I'd share what I discovered. The key for each group is the value of the state so really what I was after here was a key sort and just didn't realize it. So I found Michael Rog's "<a href="https://github.com/TopShelfCraft/SuperSort" rel="nofollow noreferrer">SuperSort</a>" plugin which has a twig filter for running a ksort and that addressed it perfectly for me making a small adjustment to use supersort in the response.</p>

<pre><code>{% set resorts = craft.entries().section('resortsList').orderBy('title ASC').all() %}
{% set resortsByState = resorts|group('address.state')|supersort('ksort') %}
</code></pre>

<p>I'm interested in other solutions too that don't require a plugin, but this particular direction got things going for me.</p>

CC BY-SA 4.0
2018-12-20T21:50:18.500Z
Nice work! Was going to respond and say exactly this. :)
Lindsey D
CC BY-SA 4.0
2018-12-20T21:51:48.157Z
There's no other solution at the moment. The address data isn't available as part of the initial SQL query, so you need to handle it separately.
Lindsey D
CC BY-SA 4.0
2018-12-20T21:53:33.040Z
Okay awesome, thank you so much Lindsey for the confirmation!
Tim Knight
CC BY-SA 4.0
2018-12-20T21:30:31.190Z
2018-12-20T22:06:23.010Z
2018-12-20T22:06:23.010Z
Tim Knight
<p>I'm working with a Structure of resort locations. Each resort has a field <code>address</code> which is a Smart Map field. I'm trying to create an initial display page that groups the resorts based on their state (a Smart Map address part), but I would like to order the output first based on the state and then by resort title.</p>
<p>Something like.</p>
<ul>
<li>Arkansas</li>
<li>Awesome Place</li>
<li>Best Place</li>
<li>Alabama</li>
<li>Alright Place</li>
<li>Better Place</li>
<li>Florida</li>
</ul>
<p>I'm partially there using the following:</p>
<pre><code>{% set resorts = craft.entries().section('resortsList').orderBy('title ASC').all() %}
{% set resortsByState = resorts|group('address.state') %}
</code></pre>
<p>And then outputting the list...</p>
<pre><code>{% for state, resortsInState in resortsByState %}
  &lt;h2&gt;{{ state }}&lt;/h2&gt;
  {% for resort in resortsInState %}
    &lt;p&gt;{{ resort.link }} - {{ resort.address.state }}&lt;/p&gt;
  {% endfor %}
{% endfor %}
</code></pre>
<p>This gives me the list grouped by the state and ordered by the resort title which gets me partially there. I've tried adding <code>address.state</code> as part of the ordering declaration, but I'm only getting errors (Unknown column 'address.state' in 'order clause'). Is there a proper way I should be doing this?</p>

<ordering><plugin-smartmap><group><elementquery>
Set OrderBy on Grouped Query Involving Smart Map Field
# Set OrderBy on Grouped Query Involving Smart Map Field

(Asked by: username_0 on 2018-12-20)

I'm working with a Structure of resort locations. Each resort has a field address which is a Smart Map field. I'm trying to create an initial display page that groups the resorts based on their state (a Smart Map address part), but I would like to order the output first based on the state and then by resort title.
Something like.

Arkansas
Awesome Place
Best Place
Alabama
Alright Place
Better Place
Florida

I'm partially there using the following:
{% set resorts = craft.entries().section('resortsList').orderBy('title ASC').all() %}
{% set resortsByState = resorts|group('address.state') %}

And then outputting the list...
{% for state, resortsInState in resortsByState %}
  {{ state }}
  {% for resort in resortsInState %}
    {{ resort.link }} - {{ resort.address.state }}
  {% endfor %}
{% endfor %}

This gives me the list grouped by the state and ordered by the resort title which gets me partially there. I've tried adding address.state as part of the ordering declaration, but I'm only getting errors (Unknown column 'address.state' in 'order clause'). Is there a proper way I should be doing this?

**Tags:** ordering, plugin-smartmap, group, elementquery


---

## 1 answers

### Answer by username_0 on 2018-12-20. Score: 2 | Accepted answer

Okay, I've been able to solve this and figured I'd share what I discovered. The key for each group is the value of the state so really what I was after here was a key sort and just didn't realize it. So I found Michael Rog's "SuperSort" plugin which has a twig filter for running a ksort and that addressed it perfectly for me making a small adjustment to use supersort in the response.
{% set resorts = craft.entries().section('resortsList').orderBy('title ASC').all() %}
{% set resortsByState = resorts|group('address.state')|supersort('ksort') %}

I'm interested in other solutions too that don't require a plugin, but this particular direction got things going for me.

> **Comments:**
> **username_1**: Nice work! Was going to respond and say exactly this. :)  
> **username_1**: There's no other solution at the moment. The address data isn't available as part of the initial SQL query, so you need to handle it separately.  
> **username_0**: Okay awesome, thank you so much Lindsey for the confirmation!  

---

