What purpose does the Comparer
class serve if the type that you specify already implements IComparable
?
If I specify Comparer.Defaul
Because you need sometimes keep sets/ordered queues ordered by something else then 'natural' order or more then one natural order exists.
For example if you have plane lines you may want to sort it by:
Tasks in computer can be scheduled by:
So even in one application you may need to sort the objects by different properties. You cannot do this by int compareTo(Object)
method as it cannot differentatie between contexts. However you can add the context i.e. implement CompareByPriority
.