title: IllegalArgumentException on Collections.sort() method
body: <p>I have comparator for strings, which are converted to date. When I pass this comparator to Collections.sort() method I get <strong>java.lang.IllegalArgumentException: Comparison method violates its general contract!</strong>.</p>
  
  <p>I have read some articles about this exception, but I don't really understand why this exception appears. Any idea ?</p>
  
  <pre><code>private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");   
  
      Comparator&lt;String&gt; comparator = new Comparator&lt;String&gt;() {
                      @Override
                      public int compare(String o1, String o2) {
                          if (o1 == null &amp;&amp; o2 == null) {
                              return 0;
                          }
                          if (o1 == null) {
                              return 1;
                          }
                          if (o2 == null) {
                              return -1;
                          }
                          try {
                              Date first = sdf.parse(o1);
                              Date second = sdf.parse(o2);
                              return first.compareTo(second);
                          } catch (Exception ignored) {
                              return 0;
                          }
                      }
                  };
  </code></pre>