<p>You can use the <a href="https://docs.craftcms.com/v2/templating/filters.html#filter" rel="nofollow noreferrer">group</a> filter and create a 2d array with the letter as the first index and the entries as second index. </p>

<pre class="lang-twig prettyprint-override"><code>{% set allEntriesByFirstCharacter = allEntries|group('companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9")' ) %}
</code></pre>

<p>The string passed in the filter will get rendered as an object template an equal results will be grouped by the same key. </p>

CC BY-SA 4.0
2018-10-30T16:57:12.677Z
Thank you! I'm getting empty outputs, but the right number of instances now. Post updated.
sarah3585
CC BY-SA 4.0
2018-10-30T16:59:38.277Z
Where do you assign the `entry` variable? You use it but I'm not sure what it should be. Please take a look at the link
Robin Schambach
CC BY-SA 4.0
2018-10-30T15:36:02.920Z
2018-10-30T15:36:02.920Z
Robin Schambach
<p>I have a A-Z menu to filter entries by their first letter.</p>

<pre><code>   &lt;ul class="directory-letters"&gt;
    &lt;li class="all current" data-filter="all"&gt;ALL&lt;/li&gt;
    {% for entry in craft.entries.section('membershipDirectory').order('title asc') %}
      &lt;li data-filter="letter-{{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}"&gt;{{ entry.companyName|slice(0, 1) }}&lt;/li&gt;
    {% endfor %}
   &lt;/ul&gt;
</code></pre>

<p>Issue I have is if there are 4 entries beginning with 'C' the output will be:</p>

<p>A B C C C C D E F...</p>

<p>Can I limit each letter once so I'll get 1 'C' is there are 1+ entries starting with 'C'?</p>

<hr>

<p>UPDATE:</p>

<p>I'm now getting the right number of instances created but no text inside my <li>. Likely a rookie error somewhere:</p>

<pre><code>{% set allEntries = 
craft.entries.section('membershipDirectory').order('title asc') %}
{% set allEntriesByFirstCharacter = 
 allEntries|group('companyName|slice(0, 1)|lower|replace("/[^a-z]/", 
 "0-9")' ) %}

 &lt;ul class="directory-letters"&gt;
    &lt;li class="all current" data-filter="all"&gt;ALL&lt;/li&gt;
    {% for allEntries in allEntriesByFirstCharacter %}
       &lt;li data-filter="letter-{{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}"&gt;{{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}&lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
</code></pre>

<entries><filter>
A-Z filter menu - limiting output to one when there are multiples
# A-Z filter menu - limiting output to one when there are multiples

(Asked by: username_0 on 2018-10-30)

I have a A-Z menu to filter entries by their first letter.
   
    ALL
    {% for entry in craft.entries.section('membershipDirectory').order('title asc') %}
      {{ entry.companyName|slice(0, 1) }}
    {% endfor %}
   

Issue I have is if there are 4 entries beginning with 'C' the output will be:
A B C C C C D E F...
Can I limit each letter once so I'll get 1 'C' is there are 1+ entries starting with 'C'?

UPDATE:
I'm now getting the right number of instances created but no text inside my . Likely a rookie error somewhere:
{% set allEntries = 
craft.entries.section('membershipDirectory').order('title asc') %}
{% set allEntriesByFirstCharacter = 
 allEntries|group('companyName|slice(0, 1)|lower|replace("/[^a-z]/", 
 "0-9")' ) %}

 
    ALL
    {% for allEntries in allEntriesByFirstCharacter %}
       {{ entry.companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9") }}
  {% endfor %}

**Tags:** entries, filter

---

## 1 answers

### Answer by username_1 on 2018-10-30. Score: 1

You can use the group filter and create a 2d array with the letter as the first index and the entries as second index. 
{% set allEntriesByFirstCharacter = allEntries|group('companyName|slice(0, 1)|lower|replace("/[^a-z]/", "0-9")' ) %}

The string passed in the filter will get rendered as an object template an equal results will be grouped by the same key.

> **Comments:**
> **username_0**: Thank you! I'm getting empty outputs, but the right number of instances now. Post updated.  
> **username_1**: Where do you assign the `entry` variable? You use it but I'm not sure what it should be. Please take a look at the link  

---

