title: Compare method throw exception: Comparison method violates its general contract
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6626437/why-does-my-compare-method-throw-exception-comparison-method-violates-its-gen">why does my compare method throw exception &mdash; Comparison method violates its general contract!</a>  </p>
  </blockquote>
  
  
  
  <p>I've this code:</p>
  
  <pre><code>class TimeComparatorTipo0 implements Comparator {
  @Override
  public int compare(Object a, Object b) {
       String Time1   = ((DataImportedTipo0) a).ora;
       Long   VolTot1 = Long.parseLong(((DataImportedTipo0) a).volume_totale);
  
       String Time2   = ((DataImportedTipo0) b).ora;
       Long   VolTot2 = Long.parseLong(((DataImportedTipo0) b).volume_totale);
  
      if (Time1.equals(Time2))
      {          
         if ( VolTot1.compareTo(VolTot2) &gt; 0)
           return 1;
         else
           return -1;         
      }
      else
        return Time1.compareTo(Time2);
       }
  };
  </code></pre>
  
  <p>Sometimes it thrown this exception:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Comparison method violates its general contract!
  at java.util.TimSort.mergeHi(TimSort.java:868)
  at java.util.TimSort.mergeAt(TimSort.java:485)
  at java.util.TimSort.mergeForceCollapse(TimSort.java:426)
  at java.util.TimSort.sort(TimSort.java:223)
  at java.util.TimSort.sort(TimSort.java:173)
  at java.util.Arrays.sort(Arrays.java:659)
  at java.util.Collections.sort(Collections.java:217)
  at ManageUrl.DownloadUrl.StartThreadDowloadTipo0(DownloadUrl.java:893)
  at ManageUrl.DownloadUpdateWorkflow$ConsumerTipo0.run(DownloadUpdateWorkflow.java:278)
  </code></pre>
  
  <p>Why ?</p>
  
  <p>1) how can I avoid it?
  2) how can i catch this exception ?</p>
  
  <p>Thanks in advance.</p>