Shapada
Shapada

Reputation: 89

Java - Sorting a Set of Points Based on Both X and Y Coordinates

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?

static public List<Point> convertToThreeByThreeGrid(String points) {
    String[] ptsArray;
    List<Point> ptsList = new ArrayList<>();
    String stripString = points.replace("[", "").replace("]", "").replace("(", "").replace(")", "").replace(" ", ",").trim();
    ptsArray = stripString.split(",");

    for(int i = 0; i < 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<Point>() {
        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<Point> convertedPtList = new ArrayList<>();
    int smallestX = (int) ptsList.get(0).getX();
    int smallestY = (int) ptsList.get(0).getY();
    for (int i = 1; i < 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;

  }
}

Output:

[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]]

[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]]

[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]]

Upvotes: 1

Views: 15633

Answers (1)

JimmyB
JimmyB

Reputation: 12610

for(int i = 0; i < ptsArray.length; i += 2) {
    int x = Integer.parseInt(ptsArray[i]);
    int y = Integer.parseInt(ptsArray[i+1]);
    ptsList.add(new Point(x, y));
}

Collections.sort( ptsList, new Comparator<Point>() {
       public int compare(Point x1, Point x2) {
         int result = Double.compare(x1.getX(), x2.getX());
         if ( result == 0 ) {
           // both X are equal -> compare Y too
           result = Double.compare(x1.getY(), x2.getY());
         } 
         return result;
      }
    });

// ptsList is now sorted by both X and Y!

Edit:

To just find the lowest X and the lowest Y you can also go the 'classic' way without any (double-)sorting:

int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;

for ( Point p : ptsList ) {

  final int x = (int)p.getX();
  final int y = (int)p.getY();

  if ( x < minX ) {
    minX = x;
  } 

  if ( y < minY ) {
    minY = y;
  }
}

Upvotes: 5

Related Questions