title: "Comparison method violates its general contract" - I cannot detect any intransitivity
body: <p>Edit: why I think this is not a duplicate: As <em>biziclop</em> wrote, the problem here is not intransitivity <code>(a&gt;b &amp; b&gt;c =&gt; a&gt;c)</code> as in the other problems mentioned here, but that the clause <code>a&gt;b =&gt; -(b&gt;a)</code> is violated, so it's a different problem.</p>
  
  <p>I am receiving the following error message:</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
  at java.util.TimSort.mergeHi(Unknown Source)
  at java.util.TimSort.mergeAt(Unknown Source)
  at java.util.TimSort.mergeForceCollapse(Unknown Source)
  at java.util.TimSort.sort(Unknown Source)
  at java.util.Arrays.sort(Unknown Source)
  at construct.Repair.regretRepair(Repair.java:101)
  at lns.One.repaired(One.java:122)
  at lns.One.segment(One.java:68)
  at lns.One.lnsSolution(One.java:35)
  at lns.All.lnsSolutions(All.java:22)
  at barter.Genetic.initialPopulation(Genetic.java:36)
  at barter.Genetic.run(Genetic.java:26)
  at program.Main.main(Main.java:22)
  </code></pre>
  
  <p>This is where it happens:</p>
  
  <pre><code>Arrays.sort(regretProfits, new Comparator&lt;RegretProfit&gt;(){
              @Override
              public int compare(RegretProfit first, RegretProfit second){
                  if (first.possibleRoutes &lt;= 0){
                      if (second.possibleRoutes &gt; 0){
                          return 1;
                      }
                      return 0;
                  }                   
                  if (first.possibleRoutes &lt; solution.schedule.size() - kay + 1){
                      if (first.possibleRoutes &lt; second.possibleRoutes){
                          return -1;
                      }
                      if (first.possibleRoutes &gt; second.possibleRoutes){
                          return 1;
                      }
                      if (first.profit &gt; second.profit){
                          return -1;
                      }
                      if (first.profit &lt; second.profit){
                          return 1;
                      }
                  }
                  if (first.regret &gt; second.regret){
                      return -1;
                  }
                  if (first.regret &lt; second.regret){
                      return 1;
                  }
                  return 0;
              }
          ;});
  </code></pre>
  
  <p>And this is the class where the object RegretProfit is defined:</p>
  
  <pre><code>public class RegretProfit {
      public int[] order;
      public double regret;
      public double profit;
      public int possibleRoutes;
  }
  </code></pre>
  
  <p>The error occurs only every few thousand iterations. I would be really thankful if anybody had some ideas what the problem might be. I've read that intransitivity can cause this exception but I really couldn't figure out where I possibly went wrong.</p>
  
  <p>Solved it, thanks to <em>biziclop</em>!</p>
  
  <pre><code>Arrays.sort(regretProfits, new Comparator&lt;RegretProfit&gt;(){
              @Override
              public int compare(RegretProfit first, RegretProfit second){
                  if (first.possibleRoutes &lt;= 0 &amp;&amp; second.possibleRoutes &gt; 0){
                      return 1;
                  }
                  if (first.possibleRoutes &lt;= 0 &amp;&amp; second.possibleRoutes &lt;= 0){
                      return 0;
                  }
                  if (first.possibleRoutes &gt; 0 &amp;&amp; second.possibleRoutes &lt;= 0){
                      return -1;
                  }
                  if (first.possibleRoutes &lt; solution.schedule.size() - kay + 1 || second.possibleRoutes &lt; solution.schedule.size() - kay + 1){
                      if (first.possibleRoutes &lt; second.possibleRoutes){
                          return -1;
                      }
                      if (first.possibleRoutes &gt; second.possibleRoutes){
                          return 1;
                      }
                      if (first.profit &gt; second.profit){
                          return -1;
                      }
                      if (first.profit &lt; second.profit){
                          return 1;
                      }
                  }
                  if (first.regret &gt; second.regret){
                      return -1;
                  }
                  if (first.regret &lt; second.regret){
                      return 1;
                  }
                  return 0;
              }
          ;});
  </code></pre>