[英]Array of Points Sorting in Java
I have a class 'Point'. 我有一个“积分”课程。 I want to sort an array of points, but I want to use 2 "compare" functions (I want to take 2 arrays, one with the points sorted by X and the other by Y).
我想对一个点数组进行排序,但是我想使用2个“比较”函数(我想使用2个数组,一个数组的点按X排序,另一个按Y排序)。 How can I make my class accept 2 comparing functions?
如何使我的班级接受2个比较函数? Here's my code
这是我的代码
static class Point implements Comparator<Point>{
int x,y;
int compareX(Point A , Point B){
return A.x - B.x;
}
int compareY(Point A , Point B){
return A.y - B.y;
}
}
Instead of having Point
implement Comparator
, have 2 Comparator
constants which compare by x and y respectively. 而不是让
Point
实现Comparator
,而是具有2个Comparator
常量,分别通过x和y进行比较。
static class Point {
public static final Comparator<Point> X_COMPARATOR = Comparator.comparingInt(Point::getX);
public static final Comparator<Point> Y_COMPARATOR = Comparator.comparingInt(Point::getY);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.