posted 14 years ago
Comparable vs Comparator
1) It is necessary to modify the class whose instance is going to be sorted. 1) A separate class can be created in order to sort the instances.
2)Only one sort sequence can be created. 2) Many sort sequence can be created.
Q1) I don't understand what these two points 1&2 mean to say in Comparator ?
Example:
Suppose Employee class which implements Comparable interface.It is sorted using empid.To sort it age wise.I have to modify this class so that it can be sorted age wise.
Q1) Does this point 1 means : You have to modify same class again to make it sorted as age wise.
Q2) Please refer Q2 below
Comparable
Comparator
Q1)A separate class can be created in order to sort the instances?
does this point means that each time you have to create different classes for different sorting requirements.
Q2) Many sort sequence can be created?
We are creating different classes for different sorting requirements. so how we can say we can create many sort sequences?What is use of adding new classes each time
as per requirement changes?
so if this is a case, we can create many sort sequences in Comparable as well.We can go on adding new classes each time as per requirement changes?