title: Getting error: Comparison method violates its general contract
body: <p>I have tried many possible solution given on the net like to set System property and to convert in double but still getting same error:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Comparison method violates its general contract!
      at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:835)
      at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:453)
      at java.util.ComparableTimSort.mergeForceCollapse(ComparableTimSort.java:392)
      at java.util.ComparableTimSort.sort(ComparableTimSort.java:191)
      at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
      at java.util.Arrays.sort(Arrays.java:472)
      at java.util.Collections.sort(Collections.java:155)
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>        System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
          Collections.sort(docs, new Comparator&lt;FeedDocument&gt;() {
              public int compare(FeedDocument o1, FeedDocument o2) {
  
                  int year1 = 0;
                  int year2 = 0;
                  int returnResult = 0;
                  if (o1.containsKey(FeedConstants.PUBLICATION_YEAR)
                          &amp;&amp; o2.containsKey(FeedConstants.PUBLICATION_YEAR)
                          &amp;&amp; o1.get(FeedConstants.PUBLICATION_YEAR) != null
                          &amp;&amp; (o1.get(FeedConstants.PUBLICATION_YEAR) instanceof String)
                          &amp;&amp; o2.get(FeedConstants.PUBLICATION_YEAR) != null
                          &amp;&amp; (o2.get(FeedConstants.PUBLICATION_YEAR) instanceof String)) {
  
                      String firstyear = (String) o1.get((FeedConstants.PUBLICATION_YEAR));
                      String secondyear = (String) o2.get((FeedConstants.PUBLICATION_YEAR));
  
                      if (firstyear.equals(secondyear)) {
                          return 0;
                      } else if (firstyear != null &amp;&amp; !firstyear.isEmpty() &amp;&amp; secondyear != null
                              &amp;&amp; !secondyear.isEmpty()) {
  
                          year1 = Integer.parseInt(firstyear.trim());
  
                          year2 = Integer.parseInt(secondyear.trim());
  
                          // int result = year2 - year1;
                          // if (result &gt; 0) {
                          // returnResult = 1;
                          // } else if (result &lt; 0) {
                          // returnResult = -1;
                          // }
                          return Double.compare(year2, year1);
                      }
  
                  } else {
                      returnResult = 0;
                  }
                  return returnResult;
              }
          });
  </code></pre>