Sort Entries by Value from Dropdown I have a list of students and can sort them by name, sport, and school. The name and sport portions are easy, I'm grabbing the values without issue.  Where I'm struggling is the school value is from a Dropdown field.
Using the following code results in no students being listed let alone being sorted by school.
   {% set usersSchoolQuery = craft.users().group('athlete').all() %}
   {% set usersBySchool = usersSchoolQuery|group('schoolAttended.one()') %}
   {% for users in usersBySchool.all() %}
   {% for user in users %} 

This should work but isn't. Could someone see where I'm going wrong here?
Edit: I've misunderstood the question! It sounds like Schools aren't related Elements, but just a dropdown value?


I think there are a couple of missteps, here:

The .group() method on a UserQuery actually applies a filter, narrowing the results by a User Group with that handle. (Maybe that's understood, but I wanted to note the difference between this and the next couple of notes!)
(An aside that will help optimize this for #3) I recommend eager-loading your related School elements by calling .with(['schoolAttended']) on your main User query.
The | group(…) filter takes an array and groups it by a property, closure, or Twig expression. In this case, I think you actually want to be more explicit about how the grouping is happening, so I'd use | group(u =&gt; (u.schoolAttended | first).id). This ensures the key used for grouping is consistent and unique.
usersBySchool.all() is calling the .all() method on an array, which won't work (it'll only be present on ElementQuery instances)—your Elements are already loaded after the first statement, and grouping just takes the resulting array and re-organizes it! This for loop should probably just be: {% for schoolId, athletes in usersBySchool %} … {% endfor %}.

So, altogether:
{# Load all “athlete” users: #}
{% set athletes = craft.users({
  group: 'athlete',
  with: ['schoolAttended']
}).all() %}

{# Group by their School's ID: #}
{% set athletesBySchoolId = athletes | group((u) =&gt; (u.schoolAttended | first).id) %}

{# Fetch Schools and index by ID. Note that we have the IDs ready, via the grouping operation, above—we can just take the keys off that array! #}
{% set schoolsById = craft.entries({
  section: 'schools',
  indexBy: 'id',
  id: athletesBySchoolId | keys
}).all() %}

{% for schoolId, athletes in athletesBySchoolId %}
  {# Look up the school for this group via our secondary Schools query (which, again, is indexed by ID): #}
  {% set school = schoolsById[schoolId] %}

  &lt;h1&gt;{{ school.title }}&lt;/h1&gt;
  &lt;ul&gt;
    {% for athlete in athletes %}
      &lt;li&gt;{{ athlete.getFullName() }}&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
{% endfor %}