Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-24T08:24:25.933Z
Answers.Text: Hi guys, and thanks. Nearly there but {{ userGroups.group }} outputs nothing.
Answers.UserDisplayName: Ian Ebden
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-23T22:33:09.287Z
Answers.LastActivityDate: 2017-05-24T07:35:16.830Z
Answers.LastEditDate: 2017-05-24T07:35:16.830Z
Answers.UserDisplayName: Mark Busnelli Jr
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2017-05-26T04:12:52.460Z
Answers.LastActivityDate: 2018-08-13T22:29:46.843Z
Answers.LastEditDate: 2018-08-13T22:29:46.843Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <users>
Title: How to list all User Groups and Users
ThreadText: # 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 %}
          <h2>{{ userGroup.name }}</h2>
          {# 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 %}
          <h2>
              {{ userGroup.name }}
          </h2>
  
          {% set users = craft.users.group(userGroup.handle) %}
  
          <ul>
          {% for user in users %}
              <li>
                  {{ user.username }}
              </li>
          {% endfor %}
          </ul>
      {% 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 %}
          <h2>{{ userGroup.name }}</h2>
          {# 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.  
  
  ---
  
  