I have a generic list manager class ListManager which I use to store Animal objects in the AnimalManager subclass to the list manager.
In the Animal class I have added the IComparer interface and added a Compare method.
For some reason i get an error in the ListManager in the sort method se code below.
in ListManager....
//Method for sorting.
public void SortComp(T aType)
{
aList.Sort(aType); //This is where I get the error
}
in Animal....
//Def field sortby enum with default value name
private SortBy compareField = SortBy.Name;
//Getter and setter
public SortBy CompareField
{
get { return compareField; }
set
{
compareField = value;
}
}
//Method (as specified by IComparer) for comparing two objects. Makes use of CompareTo method
public int Compare(Animal x, Animal y)
{
//Depending on how the comparefield is set do different sorting
switch (compareField)
{
case SortBy.Name:
return x.Name.CompareTo(y.Name);
case SortBy.ID:
return x.ID.CompareTo(y.ID);
default:
return x.Name.CompareTo(y.Name);//Just in case
}
}