title: Comparison method violates its general contract - how to avoid it
body: <p>I know there are probably a few really good explanations of that issue, but I still cannot apply it to my code. How to avoid that error in my case? Here is my code:</p>
  
  <pre><code>        sampleList.sort((o1, o2) -&gt; {
              try {
                  if (o1.getDate() == null || o2.getDate() == null) return 0;
                  int i = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(o1.getDate()).compareTo(new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(o2.getDate()));
                  return i;
              } catch (ParseException e) {
                  logger.error("Sorting failed", e);
              }
              return 0;
          });
  </code></pre>
  
  <p>sampleList is a list of type, which contains date in string. And here is an exception I get:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Comparison method violates its general contract!
      at java.util.TimSort.mergeLo(Unknown Source)
      at java.util.TimSort.mergeAt(Unknown Source)
      at java.util.TimSort.mergeCollapse(Unknown Source)
      at java.util.TimSort.sort(Unknown Source)
      at java.util.Arrays.sort(Unknown Source)
      at java.util.ArrayList.sort(Unknown Source)
      at 
  </code></pre>
  
  <p>If you have suggestions on how to solve it, please share it with me. Thank you.</p>