Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.lang.IllegalArgumentException: Comparison method violates its general contract!

ShomikApr 27 2014 — edited Apr 28 2014

public class ChromosomeRanking implements Comparable<ChromosomeRanking>{

    private double ranking;

    private Chromosome chrom;

    @Override

    public int compareTo(ChromosomeRanking cr2) {

        if(this.ranking == cr2.ranking){

            return 0;

        }

        if(this.ranking < cr2.ranking){

            return -1;

        }

        if(this.ranking > cr2.ranking){

            return 1;

        }

        return 0;

    }

  /**

   *Other methods here

   */

}

//Using above class here:

ArrayList<ChromosomeRanking> chromRankList = new ArrayList<ChromosomeRanking>();

//Lines here add members into the ArrayList

//Then calling this sort method:

Collections.sort(chromRankList);

//This code is called in numerous times while the application runs. It is running without any issue sometimes. But most of the times it is returning this exception message:

java.lang.IllegalArgumentException: Comparison method violates its general contract!

    at java.util.ComparableTimSort.mergeLo(Unknown Source)

    at java.util.ComparableTimSort.mergeAt(Unknown Source)

    at java.util.ComparableTimSort.mergeCollapse(Unknown Source)

    at java.util.ComparableTimSort.sort(Unknown Source)

    at java.util.ComparableTimSort.sort(Unknown Source)

    at java.util.Arrays.sort(Unknown Source)

    at java.util.Collections.sort(Unknown Source)

I am very much in need to resolve this issue asap. Any type of help is appreciated. Thanks.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 26 2014
Added on Apr 27 2014
2 comments
1,783 views