title: Java - Sorting a Set of Points Based on Both X and Y Coordinates
body: <p>I have a list of point objects that need to be sorted by both X and Y coordinates, but when I pass them to a comparator object only one coordinate gets sorted (the first one called). Any ideas to why this might be happening?</p>
  
  <pre><code>static public List&lt;Point&gt; convertToThreeByThreeGrid(String points) {
      String[] ptsArray;
      List&lt;Point&gt; ptsList = new ArrayList&lt;&gt;();
      String stripString = points.replace("[", "").replace("]", "").replace("(", "").replace(")", "").replace(" ", ",").trim();
      ptsArray = stripString.split(",");
  
      for(int i = 0; i &lt; ptsArray.length; i += 2) {
          int x = Integer.parseInt(ptsArray[i]);
          int y = Integer.parseInt(ptsArray[i + 1]);
          System.out.println("X: " + x);
          System.out.println("Y: " + y);
          ptsList.add(new Point(x, y));
      }
  
      Collections.sort(ptsList, new Comparator&lt;Point&gt;() {
          public int compare(Point a, Point b) {
              int result = Integer.compare((int) a.getX(), (int) b.getX());
              if (result == 0 ) {
                  result = Integer.compare((int) a.getY(), (int) b.getY());
              }
              return result;
          }
      });
  
     // subtract each coordinate by smallest x and y coordinate values
      List&lt;Point&gt; convertedPtList = new ArrayList&lt;&gt;();
      int smallestX = (int) ptsList.get(0).getX();
      int smallestY = (int) ptsList.get(0).getY();
      for (int i = 1; i &lt; ptsList.size(); i++) {
          int x = ((int) ptsList.get(i).getX() - smallestX);
          int y = ((int) ptsList.get(i).getY() - smallestY);
          convertedPtList.add(new Point(x, y));
      }
      return convertedPtList;
  
    }
  }
  </code></pre>
  
  <p>Output: </p>
  
  <p>[java.awt.Point[x=10,y=26], java.awt.Point[x=10,y=26], java.awt.Point[x=10,y=28], java.awt.Point[x=12,y=26]]</p>
  
  <p>[java.awt.Point[x=13,y=26], java.awt.Point[x=13,y=28], java.awt.Point[x=13,y=28], java.awt.Point[x=14,y=27], java.awt.Point[x=14,y=27], java.awt.Point[x=15,y=26], java.awt.Point[x=15,y=28], java.awt.Point[x=15,y=28]]</p>
  
  <p>[java.awt.Point[x=16,y=26], java.awt.Point[x=16,y=28], java.awt.Point[x=16,y=28], java.awt.Point[x=18,y=26], java.awt.Point[x=18,y=26], java.awt.Point[x=18,y=28]]</p>