<p>To group by the first letter you would simply use the <a href="http://twig.sensiolabs.org/doc/filters/slice.html" rel="nofollow"><code>slice</code></a> filter within Craft's <a href="http://buildwithcraft.com/docs/templating/filters#group" rel="nofollow"><code>group</code></a>
filter.</p>

<p>For all non letter characters you can now use Craft's <a href="http://buildwithcraft.com/docs/templating/filters#replace" rel="nofollow"><code>replace</code></a> filter (it does regex since Craft 2.2)
and replace them with whatever you want these entries to be sorted by.</p>

<pre><code>{% set entriesGrouped = entries|group('title|slice(0, 1)|upper|replace("/[^A-Z]/", "0-9")') %}

{% for letter, entriesInGroup in entriesGrouped %}
    {{ letter }}
    {% for entry in entriesInGroup %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-03-20T15:26:38.493Z
OK, that's MUCH nicer ;). Wasn't aware of the fact that slice can be used within group ...
Paul
CC BY-SA 3.0
2015-03-20T15:29:50.947Z
I found out some days ago, that you can even go crazy and do things like ternary in there: [Sorting entries A-Z, ignoring 'The' and grouping by first letter](http://craftcms.stackexchange.com/a/8665/125)
carlcs
CC BY-SA 3.0
2015-03-20T15:34:23.083Z
Nice. Very elegant!
Michael Rog
CC BY-SA 3.0
2015-03-20T15:40:23.003Z
Better to add | upper

{% set entriesGrouped = entries|group('title|slice(0, 1)|replace("/[^a-zA-Z]/", "0-9") | upper') %}
Johannes Lamers
CC BY-SA 3.0
2015-03-20T15:21:27.227Z
2015-03-20T15:43:45.650Z
2015-03-20T15:43:45.650Z
carlcs
<p>I'm trying to output a list of Brand names grouped by first letter. But some items have a number or symbol in the name. I want to list first the Numbers and symbols under 0-9 and the rest of the brands under A-Z</p>

<p>Like this:</p>

<p><strong>0-9</strong></p>

<ul>
<li>+People</li>
<li>10 Days</li>
<li>2Two</li>
<li>40 Weft</li>
<li>55 DSL</li>
<li>81hours</li>
</ul>

<p><strong>A</strong></p>

<ul>
<li>Acne</li>
<li>Adidas</li>
</ul>

<p><strong>B</strong></p>

<ul>
<li>B.Young</li>
<li>etc etc.</li>
</ul>

<templating><categories><array><group>
List Categories by 0-9 and A-Z
# List Categories by 0-9 and A-Z

(Asked by: username_0 on 2015-03-20)

I'm trying to output a list of Brand names grouped by first letter. But some items have a number or symbol in the name. I want to list first the Numbers and symbols under 0-9 and the rest of the brands under A-Z

Like this:

**0-9**

* +People
* 10 Days
* 2Two
* 40 Weft
* 55 DSL
* 81hours

**A**

* Acne
* Adidas

**B**

* B.Young
* etc etc.

**Tags:** templating, categories, array, group


---

## 1 answers

### Answer by username_1 on 2015-03-20. Score: 5 | Accepted answer

To group by the first letter you would simply use the slice filter within Craft's group
filter.
For all non letter characters you can now use Craft's replace filter (it does regex since Craft 2.2)
and replace them with whatever you want these entries to be sorted by.
{% set entriesGrouped = entries|group('title|slice(0, 1)|upper|replace("/[^A-Z]/", "0-9")') %}

{% for letter, entriesInGroup in entriesGrouped %}
    {{ letter }}
    {% for entry in entriesInGroup %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}

> **Comments:**
> **username_2**: OK, that's MUCH nicer ;). Wasn't aware of the fact that slice can be used within group ...  
> **username_1**: I found out some days ago, that you can even go crazy and do things like ternary in there: [Sorting entries A-Z, ignoring 'The' and grouping by first letter](http://craftcms.stackexchange.com/a/8665/125)  
> **username_3**: Nice. Very elegant!  
> **username_0**: Better to add | upper  {% set entriesGrouped = entries|group('title|slice(0, 1)|replace("/[^a-zA-Z]/", "0-9") | upper') %}  

---

