I am getting this error while I'm trying to sort the arraylist of nodes.I've tried most of the solutions, but none of them worked in my case.
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:866)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:483)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:406)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:213)
at java.util.Arrays.sort(Arrays.java:1312)
at java.util.Arrays.sort(Arrays.java:1506)
at java.util.ArrayList.sort(ArrayList.java:1454)
at j...
Code for this is
static class Node implements Comparable<Node>
{
int key;
int value;
int double_value;
public Node(int key , int value , int double_value)
{
this.key = key;
this.value = value;
this.double_value = double_value;
}
public int compareTo(Node node)
{
if(double_value < node.double_value)
return 1;
else if(double_value > node.double_value)
return -1;
return -1;
}
}
It works for small inputs, but when the the number of inputs are large it gives this error. I've also read about the Transitivity rule in comparison method, but I can't figure out how it is applied in this case.
Thanks in advance.