title: Java: How to sort custom type ArrayList
body: <p>I have a custom type <code>Position(x,y,z)</code>,now I create a <code>ArrayList&lt;Position&gt;</code>, i want to sort this array ordered by the value of z, from small to bigger,how can i do that using <code>Collections.sort</code>or is there any other efficient sorting method?</p>
  
  <p>When I try to use</p>
  
  <pre><code>public class PositionComparator implements Comparator&lt;Position&gt; {
  
          @Override
          public int compare(Position o1, Position o2) {
              // TODO Auto-generated method stub
              return o1.height().compareTo(o2.height());
  
          }
  
      }
  </code></pre>
  
  <p>get an error</p>
  
  <pre><code>Cannot invoke compareTo(double) on the primitive type double
  </code></pre>