Doubt About CompareTo method() ?
Dear All,
Code:
class Man
implements Comparable<Man>
{
String name;
int age;
public Man(String name,int age) {
this.age=age;
this.name=name;
}
}
This is my Man class . I have to implement the comparable interface in my Man class for sorting .I have to check both age and name in my compareTo method. For Ex if two man are same age , I have to sort depend on the name.
How can we do this ?
Code:
public int compareTo(Man obj) {
if(age > obj.age ) return 1;
else if(age < obj.age ) return -1;
else return name.compareTo(obj.name);
}
The preceding code is my try. I am not sure whether it is right or not.