I am getting a "Comparison Method violates its general contract" after compiling some Java code in Java 7, and then running it.
I have read Comparison method violates its general contract! Java 7 only and realize that there is something wrong with my code that was ignored in previous versions of Java. However I cannot work out what is wrong with my code. The Collections.sort() generates the error.
My code is:
public Comparator sortBySmoothDays() {
Comparator c = new Comparator() {
public int compare(Object arg0, Object arg1) {
Date date0 = ((PosObject)arg0).getDate();
Date date1 = ((PosObject)arg1).getDate();
double d1 = MyUtils.calcSmoothDays(date0, new Date());
double d2 = MyUtils.calcSmoothDays(date1, new Date());
if (d1 >= d2) {
return 1;
}
else {
return -1;
}
}
};
return c;
}
Comparator c = ComparatorUtils.getInstance().sortBySmoothDays();
Collections.sort(posList, c);
Can anyone help? Thank you!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…