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

SOLVED: Java Error using Arrays.sort


Exception in thread "main" java.lang.IllegalArgumentException:
Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeLo(
at java.util.ComparableTimSort.mergeAt(
at java.util.ComparableTimSort.mergeCollapse(
at java.util.ComparableTimSort.sort(
at java.util.Arrays.sort(
at Solution.main(

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){
public int compareTo(Element a){
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

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