I am using IComparable and a IComparer
In the Student class:[IComparable]
public int CompareTo(Student studentToCompare)
{
if (this.Number < studentToCompare.Number)
return 1;
else if (this.Number > studentToCompare.Number)
return -1;
else
return 0;
}
In the StudentCompareName class: [IComparer]
public int Compare(Student x, Student y)
{
return string.Compare(x.Name, y.Name);
}
With Compare(Student x, Student y) I am sorting the list of students on name.
If I dont use a CompareTo() in the Student-class, I'm getting errors.
I wonder why I need to have a CompareTo() in my main (Student) class, and what does it do? Why do I need to compare first the student number in the Student class and after that I'm allowed to sort on name in the StudentCompareName class?