title: Sort List<T> by count of property of inner list
body: <p>I'm trying to find a way to sort a list of objects I have based on a property (which is a list) of a property (another list).  Here's my objects:</p>
  
  <p><strong>ComputerCard:</strong></p>
  
  <pre><code>public partial class ComputerCard : XtraUserControl
  {
      public string ComputerName { get; set; }
      public List&lt;Role&gt; CardRoles = new List&lt;Role&gt;();
      //More properties that are omitted for brevity.
  }
  </code></pre>
  
  <p><strong>Role:</strong></p>
  
  <pre><code>public class Role
  {
      public string RoleName { get; set; }
      public string ImageName { get; set; }
      public List&lt;DependentRole&gt; DependentRoles { get; set; }
      public List&lt;AppToRun&gt; AppsToRun { get; set; }
  
      public Role()
      {
          if (AppsToRun == null)
              AppsToRun = new List&lt;AppToRun&gt;();
  
          if (DependentRoles == null)
              DependentRoles = new List&lt;DependentRole&gt;();
      }
  }
  </code></pre>
  
  <p><strong>DependentRole:</strong></p>
  
  <pre><code>public class DependentRole
  {
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>In my main form, I have declared the following:</p>
  
  <pre><code>List&lt;ComputerCard&gt; cards
  </code></pre>
  
  <p>As you can see, <code>cards</code> can have 1 or many <code>Role</code>, and each <code>Role</code> can have 0 or many <code>DependentRole</code>.  So given this list, I need a way to order it by <code>DependentRole.Count</code> in descending order.  I looked at and tried the recommendation from this <a href="https://stackoverflow.com/questions/12301814/how-to-sort-list-of-list-by-count">SO Link - How to sort list of list by count?</a> but didn't have much luck.</p>
  
  <pre><code>List&lt;ComputerCard&gt; cardz = cards.Where(c =&gt; c.LookUpEdit.EditValue != null).ToList();
  cardz.Sort((a, b) =&gt; a.CardRoles.Count - b.CardRoles.Count);
  </code></pre>
  
  <p><strong>Results:</strong></p>
  
  <pre><code>Card: File Server
    Role: Server
      Dependent Roles: 0
  
  Card: VCS Gateway
    Role: File Server
      Dependent Roles: 0
  
  Card: domain.pc1.domain
    Role: Pilot 1
      Dependent Roles: 3
  
  Card: domain.pc5.domain
    Role: Local Controller
      Dependent Roles: 1
  
  Card: domain.pc3.domain
    Role: Pilot 3
      Dependent Roles: 3
  
  Card: domain.pc4.domain
    Role: Ground Controller
      Dependent Roles: 2
  </code></pre>
  
  <p>And what I need the results to look like is this:</p>
  
  <pre><code>Card: File Server
    Role: Server
      Dependent Roles: 0
  
  Card: VCS Gateway
    Role: File Server
      Dependent Roles: 0
  
  Card: domain.pc5.domain
    Role: Local Controller
      Dependent Roles: 1
  
  Card: domain.pc4.domain
    Role: Ground Controller
      Dependent Roles: 2
  
  Card: domain.pc1.domain
    Role: Pilot 1
      Dependent Roles: 3
  
  Card: domain.pc3.domain
    Role: Pilot 3
      Dependent Roles: 3
  </code></pre>
  
  <p>Notice how the cards are listed by the count of DependentRoles.  Can this be achieved?  If so, how?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Using Amit's answer, I get the correct results but neglected the requirement of listing cards with multiple Roles in ascending order on DependentRoles.Count:</p>
  
  <pre><code>Card: File Server
      Role: Server
          Dependent Roles: 0
  
  Card: VCS Gateway
      Role: File Server
          Dependent Roles: 2
  
  Card: domain.pc1.domain
      Role: Pilot 1
          Dependent Roles: 3
  
  Card: domain.pc4.domain
      Role: Pilot 1
          Dependent Roles: 3
  
  Card: domain.pc2.domain
      Role: Pilot 1
          Dependent Roles: 3
  
  Card: domain.pc5.domain
      Role: Pilot 1
          Dependent Roles: 3
      Role: Ground Controller
          Dependent Roles: 2
  
  Card: domain.pc3.domain
      Role: Pilot 1
          Dependent Roles: 3
      Role: Ground Controller
          Dependent Roles: 2
      Role: VCS Gateway
          Dependent Roles: 0
  </code></pre>