I'm trying to get my head wrapped around the Comparable interface. I'm trying to sort by a number of things, 1.Class, 2.Company 3.EmployeeNo. I know how to sort a single characteristic, but I'm not sure how to sort 2 or more fields. The following won't work, because the obvious "if" statement is missing so it never gets to the second sorting criteria.
My thought is that I need to use if statements to see if we're company the class, company or the employee. Is that correct? If so, how would I check that? I'm also a little confused because we're comparing Strings instead of numbers (int).
public int compareTo(Object other)
{
Person that = (Person) other;
String class1 = this.getClass().getName();
String class2 = that.getClass().getName();
int result = class1.compareTo(class2);
//if(result != 0) return result; WILL THIS WORK?
String comp1 = this.company;
String comp2 = that.company;
result = comp1.compareTo(comp2);
//if(result != 0) return result; WILL THIS WORK?
String num1 = this.employeeNum;
String num2 = that.employeeNum;
result = num1.compareTo(num2);
//if(result != 0) return result; WILL THIS WORK?
return 0;
}
Thanks for the help!

New Topic/Question
Reply



MultiQuote









|