title: Java: compareTo not sorting correctly
body: <p>I have this <code>compareTo</code> code for my list:</p>
  
  <pre><code>public int compareTo(className a) 
  {               
      return (this.long1 &gt; a.long1) ? 1 : -1;
  }
  </code></pre>
  
  <p>When I use <code>Collections.sort(list)</code>, I get the following error: <code>Comparison method violates its general contract!</code></p>
  
  <p>When I change it to <code>if (this.long1 &gt;= a.long2)</code>, it works, but it does not sort properly. The longs are sorted in order, then out of order, then in order. Using <code>&gt;=</code>, the output looks like this:</p>
  
  <pre><code>...
  2000100
  2000101
  2000102
  1000100
  1000101
  2000101
  2000102
  ...
  </code></pre>
  
  <p>Now, duplicates <em>do</em> happen, and they need to be sorted correctly. It doesn't matter if the duplicates appear first or last, as long as they're properly grouped in order, like so:</p>
  
  <pre><code>...
  2000100
  2000101
  2000101
  2000102
  2000102
  1000100
  1000101
  ...
  </code></pre>
  
  <p>How would I do this properly? Thank you.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The list is still being sorted out of order with all of the below suggestions. Is this because it's a <code>List&lt;Class&gt; list = new ArrayList&lt;Class&gt;();</code>? I can't use what I'm used to from C#: <code>List&lt;Class&gt; list = new List&lt;Class&gt;()</code>. </p>