<p>Is this what you're going for?</p>

<pre><code>{% set userGroups = craft.users.getAllGroups().order('firstName ASC') %}

{% if userGroups|length %}
    {{ userGroups.group }}

    {% for userGroup in userGroups %}
        &lt;h2&gt;{{ userGroup.name }}&lt;/h2&gt;
        {# list all users for this group #}
    {% endfor %}
{% endif %}
</code></pre>

<p>This will output all of the Users in ABC order based on the <code>firstName</code> field.</p>

CC BY-SA 3.0
2017-05-24T08:24:25.933Z
Hi guys, and thanks. Nearly there but {{ userGroups.group }} outputs nothing.
Ian Ebden
CC BY-SA 3.0
2017-05-23T22:33:09.287Z
2017-05-24T07:35:16.830Z
2017-05-24T07:35:16.830Z
Mark Busnelli Jr
<p>Something like this should work:</p>

<pre><code>{% set userGroups = craft.userGroups.getAllGroups() %}

{% if userGroups|length %}
    {% for userGroup in userGroups %}
        &lt;h2&gt;
            {{ userGroup.name }}
        &lt;/h2&gt;

        {% set users = craft.users.group(userGroup.handle) %}

        &lt;ul&gt;
        {% for user in users %}
            &lt;li&gt;
                {{ user.username }}
            &lt;/li&gt;
        {% endfor %}
        &lt;/ul&gt;
    {% endfor %}
{% endif %}
</code></pre>

CC BY-SA 4.0
2017-05-26T04:12:52.460Z
2018-08-13T22:29:46.843Z
2018-08-13T22:29:46.843Z
Brad Bell
<p>I'm nearly there on this one but could use some help. I want to list all User Groups, with all Users listed under each group. For bonus points, this all needs to be alphabetical (Group, then Users). Something like...</p>

<p><strong>User Group A</strong></p>

<ul>
<li>User Name A</li>
<li>User Name B</li>
<li>etc...</li>
</ul>

<p><strong>User Group B</strong></p>

<ul>
<li>User Name A</li>
<li>User Name B</li>
<li>etc...</li>
</ul>

<p>Here's what I have so far...</p>

<pre><code>{% set userGroups = craft.userGroups.getAllGroups() %}

{% if userGroups|length %}
    {% for userGroup in userGroups %}
        &lt;h2&gt;{{ userGroup.name }}&lt;/h2&gt;
        {# list all users for this group #}
    {% endfor %}
{% endif %}
</code></pre>

<p>I just can't figure out how to loop through users for a userGroup.</p>

<users>
How to list all User Groups and Users
# How to list all User Groups and Users

(Asked by: username_0 on 2017-05-23)

I'm nearly there on this one but could use some help. I want to list all User Groups, with all Users listed under each group. For bonus points, this all needs to be alphabetical (Group, then Users). Something like...
User Group A

User Name A
User Name B
etc...

User Group B

User Name A
User Name B
etc...

Here's what I have so far...
{% set userGroups = craft.userGroups.getAllGroups() %}

{% if userGroups|length %}
    {% for userGroup in userGroups %}
        {{ userGroup.name }}
        {# list all users for this group #}
    {% endfor %}
{% endif %}

I just can't figure out how to loop through users for a userGroup.

**Tags:** users

---

## 2 answers

### Answer by username_2 on 2017-05-26. Score: 6 | Accepted answer

Something like this should work:
{% set userGroups = craft.userGroups.getAllGroups() %}

{% if userGroups|length %}
    {% for userGroup in userGroups %}
        
            {{ userGroup.name }}
        

        {% set users = craft.users.group(userGroup.handle) %}

        
        {% for user in users %}
            
                {{ user.username }}
            
        {% endfor %}
        
    {% endfor %}
{% endif %}


---

### Answer by username_1 on 2017-05-23. Score: 1

Is this what you're going for?
{% set userGroups = craft.users.getAllGroups().order('firstName ASC') %}

{% if userGroups|length %}
    {{ userGroups.group }}

    {% for userGroup in userGroups %}
        {{ userGroup.name }}
        {# list all users for this group #}
    {% endfor %}
{% endif %}

This will output all of the Users in ABC order based on the firstName field.

> **Comments:**
> **username_0**: Hi guys, and thanks. Nearly there but {{ userGroups.group }} outputs nothing.  

---

