Answers.Body: <p>Any native solution is likely to be slow - I would recommend using the Preparse field plugin.</p>
  
  <p>It lets you use Twig to dynamically generate a field at the time an entry is saved — you could have Preparse count up the number of related users on save, and then sort by that custom field.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-08T12:46:27.243Z
Answers.Text: Thanks. I ended up creating a count field which i update when new users are related.
Answers.UserDisplayName: BillyMedia
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-08T13:30:15.953Z
Answers.Text: Did you do it with Preparse? I'd recommend that over a manual text field, as the Preparse field will always recalculate based on the number of relations. The code would be as simple as: {{ entry.myRelatedUsersField|length }}
Answers.UserDisplayName: Patrick Harrington
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-08T15:28:26.973Z
Answers.Text: I read that preparse updates on entry updates, which wont work in this case as its a hidden channel storing data from an api which is populated via a custom plugin. So I added the field update to the plugin.
Answers.UserDisplayName: BillyMedia
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-08T15:49:52.120Z
Answers.Text: I believe it runs any time onSaveEntry fires, so if the custom plugin is firing the EntryModel's saveEntry method, it should run. That being said, it sounds like your way will work without issue.
Answers.UserDisplayName: Patrick Harrington
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-08T00:11:15.430Z
Answers.LastActivityDate: 2016-04-08T00:11:15.430Z
Answers.UserDisplayName: Patrick Harrington
Body: <p>I have a channel with a related users field. I want to output all channel entries ordered by the number of related users, this is what i have so far:</p>
  
  <pre><code>{%
      set entries = craft.entries.find({
          section:  'MyChannel',
          orderBy:  'relatedUsers'
      })
  %}
  </code></pre>
  
  <p>What do i do to order by the number of related users?</p>
  
Tags: <entries><relations><ordering>
Title: Order by related entries count
ThreadText: # Order by related entries count
  
  (Asked by: username_0 on 2016-04-07)
  
  I have a channel with a related users field. I want to output all channel entries ordered by the number of related users, this is what i have so far:
  
  ```
  {%
      set entries = craft.entries.find({
          section:  'MyChannel',
          orderBy:  'relatedUsers'
      })
  %}
  
  ```
  
  What do i do to order by the number of related users?
  
  **Tags:** entries, relations, ordering
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-04-08. Score: 1 | Accepted answer
  
  Any native solution is likely to be slow - I would recommend using the Preparse field plugin.
  
  It lets you use Twig to dynamically generate a field at the time an entry is saved — you could have Preparse count up the number of related users on save, and then sort by that custom field.
  
  > **Comments:**
  > **username_0**: Thanks. I ended up creating a count field which i update when new users are related.  
  > **username_1**: Did you do it with Preparse? I'd recommend that over a manual text field, as the Preparse field will always recalculate based on the number of relations. The code would be as simple as: {{ entry.myRelatedUsersField|length }}  
  > **username_0**: I read that preparse updates on entry updates, which wont work in this case as its a hidden channel storing data from an api which is populated via a custom plugin. So I added the field update to the plugin.  
  > **username_1**: I believe it runs any time onSaveEntry fires, so if the custom plugin is firing the EntryModel's saveEntry method, it should run. That being said, it sounds like your way will work without issue.  
  
  ---
  
  