Although you can get this info from javadoc, this answer will help understand the concept of compareTo() method for other users who drop by here.
When you are implementing Comparable
interface, your compareTo()
method is used to compare different objects,
public int compareTo(Franchise that) {
int stateComparition = this.getLocation().compareTo(that.getLocation());
Double sales = Double.valueOf(this.getTotal());
Double thatSales = Double.valueOf(that.getTotal());
int salesComparition = sales.compareTo(thatSales);
if(stateComparition == 0){
if(salesComparition > 0)
return -1;
else if(salesComparition < 0)
return 1;
else
return 0;
}
return stateComparition;
}
public int compareTo()
used to compare the object passed as argument with the object that calls this method.- If
this
object is less thanthat
object, a negative integer is returned, in your case its-1
. That meansthis
objects comes first beforethat
in sorted manner. - If
this
is equal tothat
object, then0
should be equal that means objects are equal. - If
this
is greater thanthat
object than a positive integer is returned, in your case, it is1
. It meansthis
comes AFTERthat
when objects ofFranchise
type are sorted.