title: Java how to sort an ArrayList of Point objects
body: <p>I'm using the Point Class to manage a list of (x,y) coordinates and I need to sort them in order of X.</p>
  
  <p>I read online to make a new class PointCompare that implements Comparator, however I'm not sure how this works and therefore I have a compiler error in the sortByXCoordinates method.</p>
  
  <p>Help would be appreciated a lot, and any comments are welcome, thanks in advance.
  Here is some of my code:</p>
  
  <pre><code>import javax.swing.JOptionPane;
  import java.awt.Point;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  //import java.util.Iterator;
  
  public class ConvexHullMain {
  
   private Point coordinates = new Point(0, 0);
   private final int MAX_POINTS = 3;
   private ArrayList&lt;Point&gt; coordinateList = new ArrayList&lt;Point&gt;();
  
   public void inputCoordinates() {
  
    String tempString; // temp string for JOptionPane
    int tempx = 0;
    int tempy = 0;
  
    for (int i = 0; i &lt; MAX_POINTS; i++) {
     try {
      // input x coordinates
      tempString = JOptionPane.showInputDialog(null,
        "Enter X coordinate:");
      tempx = Integer.parseInt(tempString);
  
      // input y coordinates
      tempString = JOptionPane.showInputDialog(null,
        "Enter Y coordinate:");
      tempy = Integer.parseInt(tempString);
  
      coordinates.setLocation(tempx, tempy);// set input data into
                // coordinates object
      coordinateList.add(coordinates.getLocation()); // put in
                  // arrayList
  
     } // end Try
     catch (NumberFormatException e) {
      System.err.println("ERROR!");
      main(null);
  
     } // end catch
  
    }// end for loop
  
   }
  
   public void displayPoints() {
  
    for (int i = 0; i &lt; MAX_POINTS; i++) {
  
     JOptionPane.showMessageDialog(null, "Point number " + (i + 1)
       + " is: " + coordinateList.get(i));
  
    }
  
    // alt method
    // Iterator i = coordinateList.iterator();
    // String outputTemp;
    // while (i.hasNext()) {
    // outputTemp = i.next().toString();
    // JOptionPane.showMessageDialog(null, "Point number " + " is: "
    // + outputTemp);
    // }
  
   }
  
  
   /**
    * This sorts the points by the X coordinates
    */
    public void sortByXCoordinates(){
  
     coordinateList.sort(coordinates, new PointCompare());
    }
  
     public class PointCompare implements Comparator&lt;Point&gt; {
  
     public int compare(Point a, Point b) {
      if (a.x &lt; b.x) {
       return -1;
      } else if (a.x &gt; b.x) {
       return 1;
      } else {
       return 0;
      }
     }
     }
  
     public static void main(String[] args) {
    ConvexHullMain main = new ConvexHullMain();
  
    main.inputCoordinates();
    main.displayPoints();
  
  
   }
  }
  </code></pre>