What purpose does the Comparer
class serve if the type that you specify already implements IComparable
?
If I specify Comparer.Defaul
public class Person
{
public string LastName;
public string FirstName;
}
public class Class2
{
public void test()
{
List classList = new List();
//add some data to the list
PersonComparer comp = new PersonComparer();
classList.Sort(comp);
}
}
public class PersonComparer : Comparer
{
public override int Compare(Person x, Person y)
{
int val = x.LastName.CompareTo(y.LastName);
if (val == 0)
{
val = x.FirstName.CompareTo(y.FirstName);
}
return val;
}
}