I'm trying to implement the same example of this link but more oriented on the number of dependent kids.
http://www.codeproject.com/Articles/42839/Sorting-Lists-using-IComparable-and-IComparer-Inte
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
But my list it's not being sorted. It remains as A: 0, B: 0, C: 2
What I'm doing wrong?
My Comparer
public class EmployeeComparer : IComparer<Employee>
{
public int Compare(Employee x, Employee y)
{
this.CompareNumberOfKids(x, y);
}
public int CompareNumberOfKids(Employee x, Employee y)
{
if (x.NumberOfKids > y.NumberOfKids)
{
return -1;
}
else if (x.NumberOfKids < y.NumberOfKids)
{
return 1;
}
else
{
return 0;
}
}
}
My Business Entity
public class Employee : IComparable<Employee>
{
//...//
Public NumberOfKids { get; set; }
int IComparable<Employee>.CompareTo(Employee next)
{
return new EmployeeComparer().Compare(this, next);
}
public override bool Equals(object obj)
{
if (obj != null && obj is Emmployee)
{
return ((Employee)obj).ID.Equals(this.ID);
}
else return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
Aspx.cs
public List<Employee> GetEmployeeSortedList()
{
List<Employee> list = new List<Employee>();
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;
}
First, there's no need to have an
IComparer<Employee>
that sorts by descending if yourEmployee
class implementsIComparable<Employee>
using the same sort criteria. And it's horribly inefficient for yourEmployee
class to instantiate a newIComparer<Employee>
for every comparision.You should change your
Employee
class so that itsCompareTo
looks like this:Then you can ditch the
EmployeeComparer
altogether and sort like this:Typically, you make the
IComparable<T>
implementation on the class perform the default sorting order. In the case of employees, that'd probably either be by employee ID or perhaps last name, first name.IComparer<T>
implementations should be for other sorting criteria.With
List<T>
, though, you have another option: use an anonymous function. For example, you could do this by writing:See this List.Sort overload.
Or, you could just ditch the whole idea of
IComparer<T>
andIComparable<T>
andList.Sort
altogether and do it the LINQ way: