I have following classes-
public class Pair
{
public double value;
int id;
Pair(){
value=0;
id=0;
}
Pair(double value, int id){
this.value=value;
this.id=id;
}
}
PairComparator class to be used for implementing Comparator class
public class PairComparator implements Comparator<Pair>{
@Override
public int compare(Pair p1, Pair p2){
return (int)(p2.value-p1.value);
}
}
I'm trying to sort a Pair collection as follows-
List<Pair> list= new ArrayList<Pair>();
list.add(new Pair(5.2, 4));
list.add(new Pair(3.4, 5));
list.add(new Pair(10.3, 3));
Collections.sort(list, new PairComparator());
However I'm getting following error-
`Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:895)
at java.util.TimSort.mergeAt(TimSort.java:512)
at java.util.TimSort.mergeCollapse(TimSort.java:435)
at java.util.TimSort.sort(TimSort.java:241)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.ArrayList.sort(ArrayList.java:1454)
at java.util.Collections.sort(Collections.java:175)
at com.research.priyanshuid.recommendation_system.MatrixRecalculation.main(MatrixRecalculation.java:59)