1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
public class Test implements Comparable<Test > {
private Float number;
private Integer weight;
private String category;
public Test() {
}
public Test(float number, int weight, float ratio, String category) {
this.number= number;
this.weight = weight;
this.category = category;
}
public String toString() {
String result = "";
result = result.concat(" -- Number-->> " + number)
.concat(" -- Weight -->> " + weight)
.concat(" -- Category -->> " + category);
return result;
}
public int compareTo(test compareTest) {
if(this.number!= compareTest.number) {
return this.number.compareTo(compareTest.number);
}
else if(this.weight!= compareTest.weight) {
return this.weight.compareTo(compareTest.weight);
}
else if(this.category != compareTest.category) {
return this.category.compareTo(compareTest.category);
}
else {
return 0;
}
}
public static Comparator<Test> comparator = new Comparator<Test>() {
public int compare(Test test1,
Test test2) {
return test1.compareTo(test2);
}
};
} |