Java - 基于 X 和 Y 坐标对一组点进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29624497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:37:35  来源:igfitidea点击:

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

javasortingpointcomparator

提问by Shapada

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?

我有一个需要按 X 和 Y 坐标排序的点对象列表,但是当我将它们传递给比较器对象时,只有一个坐标被排序(第一个被调用)。为什么会发生这种情况的任何想法?

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=10,y=26], java.awt.Point[x=10,y=26], java.awt.Point[x=10,y=28], java.awt.点[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=13,y=26], java.awt.Point[x=13,y=28], java.awt.Point[x=13,y=28], java.awt.点[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]]

[java.awt.Point[x=16,y=26], java.awt.Point[x=16,y=28], java.awt.Point[x=16,y=28], java.awt.点[x=18,y=26], java.awt.Point[x=18,y=26], java.awt.Point[x=18,y=28]]

回答by JimmyB

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:

要找到最低的 X 和最低的 Y,您也可以采用“经典”方式而无需任何(双)排序:

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;
  }
}