I am using IComparable and a IComparer
That's the source of the confusion. You only need one of the two - and they'd rarely be implemented in the same class.
IComparable<T>
is implemented to give a natural comparison between one object and another. In the case of something like Student
, that probably doesn't make sense, as there are multiple ways to compare students. On the other hand, for something like DateTime
it makes perfect sense.
IComparer<T>
is meant to be a custom comparison - so it makes sense to have a StudentNameComparer
which implements IComparer<Student>
by comparing the names, for example.