Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Java Error using Arrays.sort

user3422177:


Exception in thread "main" java.lang.IllegalArgumentException:
Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeLo(ComparableTimSort.java:744)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:481)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:406)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:213)
at java.util.Arrays.sort(Arrays.java:1246)
at Solution.main(Solution.java:16)

This is the exception when I am running my program. Error occurs when I am using Arrays.sort function on an array of objects of class Element described as


static class Element implements Comparable{
int pos,val;
Element(int pos,int val){
this.pos=pos;
this.val=val;
}
public int compareTo(Element a){
if(val!=a.val)
return val-a.val;
return pos-a.pos;
}
}

It runs perfectly fine in some cases but throws the above error in other cases. What is the error in my program?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Java Error using Arrays.sort

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×