<p>The way to do this is with the <a href="https://craftcms.com/docs/templating/filters#group" rel="nofollow noreferrer">group</a> filter. You want to group the entries by the first character of the title.</p>

<p>Get the entries as an actual array</p>

<pre><code>{% set allEntries = craft.entries.section('artists').order('title').find %}
</code></pre>

<p>Group them by first letter of title. <a href="https://twig.symfony.com/doc/2.x/filters/first.html" rel="nofollow noreferrer">first</a> will return the first character of a string. <a href="https://twig.symfony.com/doc/2.x/filters/upper.html" rel="nofollow noreferrer">upper</a> converts a string to uppercase.</p>

<pre><code>{% set groupedEntries = allEntries|group('title|first|upper') %}
</code></pre>

<p>Now you can loop through them:</p>

<pre><code>{% for index, values in groupedEntries %}
  &lt;h2&gt;{{index}}&lt;/h2&gt;
  &lt;ul class="sub-nav"&gt;
    {% for e in values %}
      &lt;li&gt;...{{e}}...&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
{% endfor %}
</code></pre>

<p>See also <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">this question</a>.</p>

CC BY-SA 3.0
2017-10-16T13:30:11.280Z
Thanks! Edited that slightly to:
{% set entriesGrouped = allEntries|group('title|slice(0, 1)|upper|replace("/[^A-Z]/", "0-9")') %}
to get 0-9 rather then 0,1,2 etc.
sarah3585
CC BY-SA 3.0
2017-10-16T12:57:10.367Z
2017-10-16T12:57:10.367Z
Marion Newlevant
<p>I'm created an Index like page (like at the back of a book) for entries within a certain section, ordered alphabetically with the letter proceeding it.</p>

<p>My code below seems like a tedious way to do it, is their an easier way? </p>

<p>How would I handle the entries with a number as the first character (0-9) of the title?</p>

<pre><code>     &lt;h2&gt;A&lt;/h2&gt;

     &lt;ul class="sub-nav"&gt;
      {% for entry in craft.entries.section('artists').search('title::A*').order('title').find() %}
        &lt;li&gt;...&lt;/li&gt;
        {% endfor %}
      &lt;/ul&gt;

    &lt;h2&gt;B&lt;/h2&gt;

     &lt;ul class="sub-nav"&gt;
      {% for entry in craft.entries.section('artists').search('title::B*').order('title').find() %}
        &lt;li&gt;...&lt;/li&gt;
        {% endfor %}
      &lt;/ul&gt;


    &lt;h2&gt;C&lt;/h2&gt;

     &lt;ul class="sub-nav"&gt;
      {% for entry in craft.entries.section('artists').search('title::C*').order('title').find() %}
        &lt;li&gt;...&lt;/li&gt;
        {% endfor %}
      &lt;/ul&gt;
</code></pre>

<templating><navigation><order>
Creating a sitemap/index page, ordered by A-Z and 0-9
# Creating a sitemap/index page, ordered by A-Z and 0-9

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

I'm created an Index like page (like at the back of a book) for entries within a certain section, ordered alphabetically with the letter proceeding it.
My code below seems like a tedious way to do it, is their an easier way? 
How would I handle the entries with a number as the first character (0-9) of the title?
     A

     
      {% for entry in craft.entries.section('artists').search('title::A*').order('title').find() %}
        ...
        {% endfor %}
      

    B

     
      {% for entry in craft.entries.section('artists').search('title::B*').order('title').find() %}
        ...
        {% endfor %}
      


    C

     
      {% for entry in craft.entries.section('artists').search('title::C*').order('title').find() %}
        ...
        {% endfor %}

**Tags:** templating, navigation, order

---

## 1 answers

### Answer by username_1 on 2017-10-16. Score: 4 | Accepted answer

The way to do this is with the group filter. You want to group the entries by the first character of the title.
Get the entries as an actual array
{% set allEntries = craft.entries.section('artists').order('title').find %}

Group them by first letter of title. first will return the first character of a string. upper converts a string to uppercase.
{% set groupedEntries = allEntries|group('title|first|upper') %}

Now you can loop through them:
{% for index, values in groupedEntries %}
  {{index}}
  
    {% for e in values %}
      ...{{e}}...
    {% endfor %}
  
{% endfor %}

See also this question.

> **Comments:**
> **username_0**: Thanks! Edited that slightly to: {% set entriesGrouped = allEntries|group('title|slice(0, 1)|upper|replace("/[^A-Z]/", "0-9")') %} to get 0-9 rather then 0,1,2 etc.  

---

