title: How to sort a list of objects with IComparable and IComparer
body: <p>I'm trying to implement the same example of this link but more oriented on the number of dependent kids.</p>
  
  <p><a href="http://www.codeproject.com/Articles/42839/Sorting-Lists-using-IComparable-and-IComparer-Inte" rel="nofollow">http://www.codeproject.com/Articles/42839/Sorting-Lists-using-IComparable-and-IComparer-Inte</a></p>
  
  <p>So I have 3 employees with A: 0, B: 0, C: 2 respectively. I want to order them descendently by number of kids. So I would have C:2, B:0, A:0</p>
  
  <p>But my list it's not being sorted. It remains as A: 0, B: 0, C: 2</p>
  
  <p>What I'm doing wrong?</p>
  
  <p><strong>My Comparer</strong></p>
  
  <pre><code>public class EmployeeComparer : IComparer&lt;Employee&gt;
  {
      public int Compare(Employee x, Employee y)
      {
          this.CompareNumberOfKids(x, y);
      }
  
      public int CompareNumberOfKids(Employee x, Employee y)
      {
          if (x.NumberOfKids &gt; y.NumberOfKids)
          {
              return -1;
          }
          else if (x.NumberOfKids &lt; y.NumberOfKids)
          {
              return 1;
          }
          else
          {
              return 0;
          }
      }
  }
  </code></pre>
  
  <p><strong>My Business Entity</strong></p>
  
  <pre><code>public class Employee : IComparable&lt;Employee&gt;
  {
      //...//
      Public NumberOfKids { get; set; }
  
      int IComparable&lt;Employee&gt;.CompareTo(Employee next)
      {
          return new EmployeeComparer().Compare(this, next);
      }
  
      public override bool Equals(object obj)
      {
          if (obj != null &amp;&amp; obj is Emmployee)
          {
              return ((Employee)obj).ID.Equals(this.ID);
          }
          else return base.Equals(obj);
      }
  
      public override int GetHashCode()
      {
          return base.GetHashCode();
      }
  }
  </code></pre>
  
  <p><strong>Aspx.cs</strong></p>
  
  <pre><code>public List&lt;Employee&gt; GetEmployeeSortedList()
  {
      List&lt;Employee&gt; list = new List&lt;Employee&gt;();
      list.Add(new Employee() { Name = "A", NumberOfKids = 0 } );
      list.Add(new Employee() { Name = "B", NumberOfKids = 0 } );
      list.Add(new Employee() { Name = "C", NumberOfKids = 2 } );
      list.Add(new Employee() { Name = "D", NumberOfKids = 1 } );
      list.Add(new Employee() { Name = "E", NumberOfKids = 0 } );
      list.Add(new Employee() { Name = "F", NumberOfKids = 4 } );
  
      list = list.Take(3).ToList();
      EmployeeComparer comp = new EmployeeComparer();
      list.Sort(comp);
      return list;
  }
  </code></pre>