Answers.Body: <p>You could use a <a href="https://github.com/aelvan/Preparse-Field-Craft" rel="nofollow">Preparse field</a> to calculate and store that value.</p>
  
  <p>The only downside is, it'll only do that calculation when you save the <strong>parent</strong> entry. So if you add another descendant, you'd have to go back and re-save the parent entry to re-calculate the value.</p>
  
  <blockquote>
    <p><strong>Pro tip:</strong> You can go to <code>Settings &gt; Sections</code> and re-save the section you are working with. This will automatically trigger a re-save of every single entry in that section.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-31T21:33:27.407Z
Answers.Text: I'd like to upvote with half a point for this half baked solution. But as it's probably the only way to approach that problem without custom sql, I'll give you the full upvote. :)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-31T21:37:19.267Z
Answers.Text: Lol, thanks @carlcs. It's the only way I could think of without introducing something like template hooks.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-01T18:22:38.350Z
Answers.Text: I did come across the Preparse Field plugin while searching for a solution. Since I want this process to be automated, I’m guessing my best bet it to write a custom plugin where I latch on to a hook and trigger a re-save on the parent everytime an entry is modified in that section.
Answers.UserDisplayName: rungta
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-01T19:51:17.123Z
Answers.Text: @rungta: That sounds like a good solution!
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-31T17:48:55.847Z
Answers.LastActivityDate: 2016-03-31T17:48:55.847Z
Answers.UserDisplayName: Lindsey D
Body: <p>I want to sort (and paginate) entries from a structure section based on the number of descendants. This is the effective criteria I want to query:</p>
  
  <pre><code>craft.entries.section('myStructure').level(1).order('descendants desc')
  </code></pre>
  
  <p>Since <code>getDescendants.count()</code> is not a number stored against each entry in the database (AFAIK), how does one go about getting this result?</p>
  
Tags: <structure><sorting>
Title: How to sort entries by their descendant count?
ThreadText: # How to sort entries by their descendant count?
  
  (Asked by: username_0 on 2016-03-31)
  
  I want to sort (and paginate) entries from a structure section based on the number of descendants. This is the effective criteria I want to query:
  
  ```
  craft.entries.section('myStructure').level(1).order('descendants desc')
  
  ```
  
  Since `getDescendants.count()` is not a number stored against each entry in the database (AFAIK), how does one go about getting this result?
  
  **Tags:** structure, sorting
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-03-31. Score: 2 | Accepted answer
  
  You could use a [Preparse field](https://github.com/aelvan/Preparse-Field-Craft) to calculate and store that value.
  
  The only downside is, it'll only do that calculation when you save the **parent** entry. So if you add another descendant, you'd have to go back and re-save the parent entry to re-calculate the value.
  
  > **Pro tip:** You can go to `Settings > Sections` and re-save the section you are working with. This will automatically trigger a re-save of every single entry in that section.
  
  > **Comments:**
  > **username_2**: I'd like to upvote with half a point for this half baked solution. But as it's probably the only way to approach that problem without custom sql, I'll give you the full upvote. :)  
  > **username_1**: Lol, thanks @username_2. It's the only way I could think of without introducing something like template hooks.  
  > **username_0**: I did come across the Preparse Field plugin while searching for a solution. Since I want this process to be automated, I’m guessing my best bet it to write a custom plugin where I latch on to a hook and trigger a re-save on the parent everytime an entry is modified in that section.  
  > **username_1**: @username_0: That sounds like a good solution!  
  
  ---
  
  