How do I use a public int compareTo method to compare two objects? I need to compare the values within the object to the values of another object to test for greater/less than. So comparing a in 2 objects and b in two objects respectfully.
Test x1 = new Test(9999,9999);
Test x2 = new Test(0,0);
public class Test{
private int a;
private int b;
public Test(){
a = 0;
b = 0;
}
public Test(int a, int b){
this.a = a;
this.b = b;
}
}
Implements
Compareable<T>interface to classTest.Compareableinterface is having a single methodcomapreTowhich accepts the object to be compared.This interface actually imposes a total ordering on the objects in collection of each class that implements it. This ordering is referred to as the class’s natural ordering, and the class’s
compareTomethod is referred to as itsnatural comparisonmethod. Which helps Collection of Object to sort in certain order.