<p>First, sort all entries by multiple criteria:</p>

<pre><code>{# get our entries and order by firstname, lastname #}
{% set allEntries = craft.entries.section('entries').orderBy('lastname asc, firstname asc').all() %}
</code></pre>

<p>Then use craft's custom "group" filter (<code>lastname[:1]</code> picks the first letter):</p>

<pre><code>{# group them by the first letter of the last name https://docs.craftcms.com/v3/dev/filters.html#group #}
{% set allEntriesByGroup = allEntries | group('lastname[:1]') %}
</code></pre>

<p>Then loop through that multidimensional array (<code>firstletter, entriesInGroup</code>) is <code>key -&gt; value</code> in twig:</p>

<pre><code>{% for firstletter, entriesInGroup in allEntriesByGroup %}
    &lt;h3&gt;{{firstletter}}&lt;/h3&gt;
    &lt;ul&gt;
    {% for entry in entriesInGroup %}
       &lt;li&gt;{{entry.lastname}}, {{entry.firstname}}&lt;/li&gt;
    {% endfor %}
    &lt;/ul&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-08-26T06:43:04.040Z
The reason I'm posting it is I first thought I had to do the array manipulation in twig – but then found out twig doesn't really support that, by design – and finally, found out craft provides a great set of tools on top of twig like those custom filters
Urs
CC BY-SA 4.0
2018-08-27T02:02:53.920Z
Here's another option from a previous SE thread: https://craftcms.stackexchange.com/a/22397/278 (not updated for Craft 3).
CraftQuest
CC BY-SA 4.0
2018-08-26T06:41:07.053Z
2018-08-26T06:52:34.790Z
2018-08-26T06:52:34.790Z
Urs
<p>I would like to build an alphabetical list of craft entries, but group them by the initials of one field of each entry. Thus, getting some kind of alphabetical index, like</p>

<p>A </p>

<ul>
<li>Apples</li>
<li>Ananas</li>
</ul>

<p>B</p>

<ul>
<li>Beer</li>
</ul>

<p>How do I do that?</p>

<templating><filter><array>
Alphabetical index for a list of entries
# Alphabetical index for a list of entries

(Asked by: username_0 on 2018-08-26)

I would like to build an alphabetical list of craft entries, but group them by the initials of one field of each entry. Thus, getting some kind of alphabetical index, like

A

* Apples
* Ananas

B

* Beer

How do I do that?

**Tags:** templating, filter, array


---

## 1 answers

### Answer by username_0 on 2018-08-26. Score: 10 | Accepted answer

First, sort all entries by multiple criteria:
{# get our entries and order by firstname, lastname #}
{% set allEntries = craft.entries.section('entries').orderBy('lastname asc, firstname asc').all() %}

Then use craft's custom "group" filter (lastname[:1] picks the first letter):
{# group them by the first letter of the last name https://docs.craftcms.com/v3/dev/filters.html#group #}
{% set allEntriesByGroup = allEntries | group('lastname[:1]') %}

Then loop through that multidimensional array (firstletter, entriesInGroup) is key -> value in twig:
{% for firstletter, entriesInGroup in allEntriesByGroup %}
    {{firstletter}}
    
    {% for entry in entriesInGroup %}
       {{entry.lastname}}, {{entry.firstname}}
    {% endfor %}
    
{% endfor %}

> **Comments:**
> **username_0**: The reason I'm posting it is I first thought I had to do the array manipulation in twig – but then found out twig doesn't really support that, by design – and finally, found out craft provides a great set of tools on top of twig like those custom filters  
> **username_1**: Here's another option from a previous SE thread: https://craftcms.stackexchange.com/a/22397/278 (not updated for Craft 3).  

---

