<p>You can do this with the <a href="https://github.com/victor-in/Craft-TwigPCRE/">twigPCRE</a> filter. Once you have your <code>allTags</code>, group them like this:</p>

<pre><code>{% set allTagsGrouped = allTags|group('name|first|upper|preg_replace("/[A-M]/", "A-M", 1)|preg_replace("/[N-Z]/", "N-Z", 1)') %}
</code></pre>

<p>This takes the tag name, gets the first character, converts it to uppercase (so 'a' and 'A' are grouped together), and then maps all characters in the range A-M to the string "A-M", and all characters in the range N-Z to the string "N-Z". So <code>allTagsGrouped</code> will have two keys: <em>"A-M"</em>, and <em>"N-Z"</em> (assuming all tag names start with letters). Obviously you could use more ranges.</p>

<p>Ordering <code>allTags</code> by name appears to be a case insensitive sort, so <code>allTagsGrouped</code> should still be in the right order.</p>

CC BY-SA 3.0
2014-12-29T21:33:10.333Z
Got around to implementing this and it worked perfectly. Thanks!
Adam Snetman
CC BY-SA 3.0
2014-12-27T03:01:29.443Z
2014-12-27T03:01:29.443Z
Marion Newlevant
<p>I'd like to be able to fetch a list of tags from a blog section and then group them into ranges of letters.</p>

<p>I've used this code to fetch and group a list of tags by first letter:</p>

<pre><code>{% set entries = craft.entries.section('news') %}

{% set allTags = craft.tags.relatedTo({
  sourceElement: entries
}).order('name').limit(null) %}

{% set allTagsGrouped = allTags|group('name | first') %}
</code></pre>

<p>But what I'd like to do is to have groups for ranges of first letters like A–E, F–J, etc.</p>

<p>The end goal is to create an index of all of the tags in use, but group them under ranged letter headers instead of individual letter headers.</p>

<p>How would I go about doing this?</p>

<p>Thanks!</p>

<tags><group><range>
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.)?

(Asked by: username_0 on 2014-12-26)

I'd like to be able to fetch a list of tags from a blog section and then group them into ranges of letters.
I've used this code to fetch and group a list of tags by first letter:
{% set entries = craft.entries.section('news') %}

{% set allTags = craft.tags.relatedTo({
  sourceElement: entries
}).order('name').limit(null) %}

{% set allTagsGrouped = allTags|group('name | first') %}

But what I'd like to do is to have groups for ranges of first letters like A–E, F–J, etc.
The end goal is to create an index of all of the tags in use, but group them under ranged letter headers instead of individual letter headers.
How would I go about doing this?
Thanks!

**Tags:** tags, group, range

---

## 1 answers

### Answer by username_1 on 2014-12-27. Score: 6 | Accepted answer

You can do this with the twigPCRE filter. Once you have your allTags, group them like this:
{% set allTagsGrouped = allTags|group('name|first|upper|preg_replace("/[A-M]/", "A-M", 1)|preg_replace("/[N-Z]/", "N-Z", 1)') %}

This takes the tag name, gets the first character, converts it to uppercase (so 'a' and 'A' are grouped together), and then maps all characters in the range A-M to the string "A-M", and all characters in the range N-Z to the string "N-Z". So allTagsGrouped will have two keys: "A-M", and "N-Z" (assuming all tag names start with letters). Obviously you could use more ranges.
Ordering allTags by name appears to be a case insensitive sort, so allTagsGrouped should still be in the right order.

> **Comments:**
> **username_0**: Got around to implementing this and it worked perfectly. Thanks!  

---

