2 Replies Latest reply: Apr 28, 2014 10:04 AM by rp0428 RSS

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

    Shomik

      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.

        • 1. Re: java.lang.IllegalArgumentException: Comparison method violates its general contract!
          baftos

          I would check if you ever reach the final return 0 of your compareTo(). Apparently you should not, so if you reach it, maybe it's better to throw an exception for troubleshooting purposes. What I suspect is that sometimes one of the doubles to compare is Double.NaN, which according to the JLS "All numeric operations with NaN as an operand produce NaN as a result. As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN". If this is the case, you should check where do you produce a Double.NaN as ranking. The JLS again: "an operation that has no mathematically definite result produces NaN". You may also try to use Double.isNan() and Double.compareTo(), which hints that relaying on comparison operators with doubles is not quite right.

          • 2. Re: java.lang.IllegalArgumentException: Comparison method violates its general contract!
            rp0428

            Post the rest of the code. You haven't posted any code that shows how 'ranking' is getting populated, how the list is getting populated.

            But most of the times it is returning this exception message:

            How many items are in the list being sorted? Before sorting iterate the list and print out all of the 'ranking' values so you can see what they are.