<p>I ended up using a combination of solutions provided:</p>

<pre><code> {% set groupedShops = craft.entries.section('shops').type('shops')
                    .order('title').all() |
                    group("title|first|upper |
                    replace('/[A-F]/', 'A to F', 1) |
                    replace('/[G-L]/', 'G to L', 1) |
                    replace('/[M-R]/', 'M to R', 1) |
                    replace('/[S-Z]/', 'S to Z', 1)") %}
</code></pre>

<p>Then you can do:</p>

<pre><code>  {% for groupName, group in groupedShops %}
      &lt;h1&gt; {{ groupName }} &lt;/h1&gt;
      &lt;ul&gt;
        {% for shop in group %}
          &lt;li&gt; {{ shop.title }} &lt;/li&gt;
        {% endfor %}
      &lt;/ul&gt;
  {% endfor %}
</code></pre>

<p>Then you have:</p>

<p><strong>A to F:</strong></p>

<ul>
<li>Amazon</li>
<li>Best Buy</li>
</ul>

<p><strong>G to L:</strong></p>

<ul>
<li>Happy Shops</li>
<li>Hollister</li>
<li>Luxe DH</li>
</ul>

<p>...etc</p>

<p>A very similar query is explained here:</p>

<p><a href="https://craftcms.stackexchange.com/questions/5404/how-can-i-group-a-list-of-tags-into-ranges-of-first-letters-a-e-f-j-etc">How can I group a list of tags into ranges of first letters (A–E, F–J, etc.)?</a></p>

<p>Important detail pointed by @Carlcs:</p>

<blockquote>
  <p>In Craft 3 you need to explicitly query for the entries using <strong>all()</strong> method on the elment query object</p>
</blockquote>

CC BY-SA 3.0
2017-10-26T15:34:49.130Z
2017-10-26T15:34:49.130Z
Rosana Ruiz
<p>Not exactly sure what you are trying to do with this data, but it sounds like you could use the <a href="https://craftcms.com/docs/templating/filters#group" rel="nofollow noreferrer">group filter</a></p>

<pre><code>{% set alphaEntries = craft.entries.section('shops').type('shops').all() | group('title | slice(0, 1)') %}

{% for letter, entries in alphaEntries %}
    &lt;h2&gt;{{ letter }}&lt;/h2&gt;
    &lt;ul&gt;
        {% for entry in entries %}
            &lt;li&gt;{{ entry.title }}&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-10-26T08:09:38.507Z
Hi Trevor, thanks for answering. What I need is to group shops into alphabetical groups like:
_From A to F_:
Amazon, Best Buy, Carrefour
_From G to L_:
H&M, IceLand
... etc
The answer you gave me throws the error: **Argument 1 passed to craft\web\twig\Extension::groupFilter() must be of the type array**..
Rosana Ruiz
CC BY-SA 3.0
2017-10-26T12:36:46.470Z
@RosanaRuiz in Craft 3 you need to explicitly query for the entries using `all()` method on the elment query object. That change should make the `group` filter happy!
carlcs
CC BY-SA 3.0
2017-10-25T20:30:33.427Z
2017-10-26T12:32:28.150Z
2017-10-26T12:32:28.150Z
Trevor Davis
<p>I need to group a set of entries alphabetically and I don't get a working and clean solution.
Taking <a href="https://craftcms.com/docs/searching" rel="nofollow noreferrer">this documentation entry</a>, I am doing it as follows:</p>

<pre><code>  {% set fromAtoF = craft.entries.section('shops').type('shops').order('title').search('title:a* OR A* OR b* OR B* OR c* OR C* OR d* OR D* OR e* OR E* OR f* OR F*') %}
  {% set fromGtoL = craft.entries.section('shops').type('shops').order('title').search('title:g* OR G* OR h* OR H* OR i* OR I* OR j* OR J* OR k* OR K* OR l* OR L*') %}
  {% set fromMtoR = craft.entries.section('shops').type('shops').order('title').search('title:m* OR M* OR n* OR N* OR o* OR O* OR p* OR P* OR q* OR Q* OR r* OR R*') %}
  {% set fromStoZ = craft.entries.section('shops').type('shops').order('title').search('title:s* OR S* OR t* OR T* OR u* OR U* OR v* OR V* OR w* OR W* OR x* OR X* OR y* OR Y* OR z* OR Z*') %}
</code></pre>

<ul>
<li><strong>Problem 1</strong>: I get entries starting by 'A' like 'Amazon' in the <code>fromStoZ</code> group, and 'Walmart' in <code>fromGToL</code>...etc</li>
<li><strong>Problem 2</strong>: It's not clean nor elegant. There must be other ways to query this but I haven't found the good one yet.</li>
</ul>

<p>I also tried:</p>

<pre><code>  {% set fromAtoF = craft.entries.section('shops').type('shops').order('title').search('title:a* OR b* OR title:c* OR title:d* OR title:e* OR title:f*') %}
  {% set fromGtoL = craft.entries.section('shops').type('shops').order('title').search('title:g* OR title:h* OR title:i* OR title:j* OR title:k* OR title:l*') %}
  {% set fromMtoR = craft.entries.section('shops').type('shops').order('title').search('title:m* OR title:n* OR title:o* OR title:p* OR title:q* OR title:r*') %}
  {% set fromStoZ = craft.entries.section('shops').type('shops').order('title').search('title:s* OR title:t* OR title:u* OR title:w* OR title:x* OR title:y* OR title:z*') %}
</code></pre>

<p>But neither it works; entries like 'Best Buy' and 'JC Penney' don't get into any group, but 'Best Buy2' goes correctly into fromAtoF</p>

<search><craft3>
Grouping entries by title in Craft 3
# Grouping entries by title in Craft 3

(Asked by: username_0 on 2017-10-25)

I need to group a set of entries alphabetically and I don't get a working and clean solution.
Taking this documentation entry, I am doing it as follows:
  {% set fromAtoF = craft.entries.section('shops').type('shops').order('title').search('title:a* OR A* OR b* OR B* OR c* OR C* OR d* OR D* OR e* OR E* OR f* OR F*') %}
  {% set fromGtoL = craft.entries.section('shops').type('shops').order('title').search('title:g* OR G* OR h* OR H* OR i* OR I* OR j* OR J* OR k* OR K* OR l* OR L*') %}
  {% set fromMtoR = craft.entries.section('shops').type('shops').order('title').search('title:m* OR M* OR n* OR N* OR o* OR O* OR p* OR P* OR q* OR Q* OR r* OR R*') %}
  {% set fromStoZ = craft.entries.section('shops').type('shops').order('title').search('title:s* OR S* OR t* OR T* OR u* OR U* OR v* OR V* OR w* OR W* OR x* OR X* OR y* OR Y* OR z* OR Z*') %}


Problem 1: I get entries starting by 'A' like 'Amazon' in the fromStoZ group, and 'Walmart' in fromGToL...etc
Problem 2: It's not clean nor elegant. There must be other ways to query this but I haven't found the good one yet.

I also tried:
  {% set fromAtoF = craft.entries.section('shops').type('shops').order('title').search('title:a* OR b* OR title:c* OR title:d* OR title:e* OR title:f*') %}
  {% set fromGtoL = craft.entries.section('shops').type('shops').order('title').search('title:g* OR title:h* OR title:i* OR title:j* OR title:k* OR title:l*') %}
  {% set fromMtoR = craft.entries.section('shops').type('shops').order('title').search('title:m* OR title:n* OR title:o* OR title:p* OR title:q* OR title:r*') %}
  {% set fromStoZ = craft.entries.section('shops').type('shops').order('title').search('title:s* OR title:t* OR title:u* OR title:w* OR title:x* OR title:y* OR title:z*') %}

But neither it works; entries like 'Best Buy' and 'JC Penney' don't get into any group, but 'Best Buy2' goes correctly into fromAtoF

**Tags:** search, craft3

---

## 2 answers

### Answer by username_0 on 2017-10-26. Score: 3 | Accepted answer

I ended up using a combination of solutions provided:
 {% set groupedShops = craft.entries.section('shops').type('shops')
                    .order('title').all() |
                    group("title|first|upper |
                    replace('/[A-F]/', 'A to F', 1) |
                    replace('/[G-L]/', 'G to L', 1) |
                    replace('/[M-R]/', 'M to R', 1) |
                    replace('/[S-Z]/', 'S to Z', 1)") %}

Then you can do:
  {% for groupName, group in groupedShops %}
       {{ groupName }} 
      
        {% for shop in group %}
           {{ shop.title }} 
        {% endfor %}
      
  {% endfor %}

Then you have:
A to F:

Amazon
Best Buy

G to L:

Happy Shops
Hollister
Luxe DH

...etc
A very similar query is explained here:
How can I group a list of tags into ranges of first letters (A–E, F–J, etc.)?
Important detail pointed by @Carlcs:

In Craft 3 you need to explicitly query for the entries using all() method on the elment query object


---

### Answer by username_1 on 2017-10-25. Score: 4

Not exactly sure what you are trying to do with this data, but it sounds like you could use the group filter
{% set alphaEntries = craft.entries.section('shops').type('shops').all() | group('title | slice(0, 1)') %}

{% for letter, entries in alphaEntries %}
    {{ letter }}
    
        {% for entry in entries %}
            {{ entry.title }}
        {% endfor %}
    
{% endfor %}

> **Comments:**
> **username_0**: Hi Trevor, thanks for answering. What I need is to group shops into alphabetical groups like: _From A to F_: Amazon, Best Buy, Carrefour _From G to L_: H&M, IceLand ... etc The answer you gave me throws the error: **Argument 1 passed to craft\web\twig\Extension::groupFilter() must be of the type array**..  
> **username_2**: @RosanaRuiz in Craft 3 you need to explicitly query for the entries using `all()` method on the elment query object. That change should make the `group` filter happy!  

---

