Everything seemed to be running okay (for several days), but I ran into an issue only once and having a really hard time to reproduce the problem.
"Comparison method violates its general contract!" was thrown and completely caught me off guard. I have the following:
public class CustomComparator implements Comparator<Chromosome> {
public int compare(Chromosome c1, Chromosome c2){
return c1.compareTo(c2);
}
}
My Chromosome class:
public class Chromosome implements Comparable<Chromosome>{
private double rank;
//bunch of methods...
@Override public int compareTo(Chromosome c){
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;
if (this.getRank() == c.getRank()) //getRank() simply returns a double value 'rank'
return EQUAL;
else if (this.getRank() < c.getRank())
return BEFORE;
else //i.e. (this.getRank() > c.getRank())
return AFTER;
}
I have an ArrayList and I used both Collections.sort(MyList) and Collections.sort(MyList, Collections.reverseOrder()). They're still working fine up till now. I just ran into that error only once out of 100's of run. Is there something wrong with this implementation?