I have recently started learning Java. I was looking at Comparable and Comparator. One of the difference I read was "when you have one sorting criteria, use comparable interface and for more than one use Comparator". But for Comparable I can use multiple sorting criteria's too like below :
class Employee implements Comparable<Employee>{
public int empID;
public int empStartDate;
public int compareTo(Employee empObj){
int result = this.empID - empObj.empID;
if(result == 0){
result = this.empStartDate - empObj.empStartDate;
}
return result;
}
}
I do understand other differences they have. But I am kind of stuck on this difference (Single and multiple sorting criteria). Please correct me if I am wrong. Why is Comparable recommended for single sorting criteria if we can do using above approach? Could someone please explain me with an example? Thank you!