Sorting point lists using java

See English answers > sort ArrayList of custom objects by property 25

Solution

Yes, create a custom comparator and use it to sort the list of points

class Point{
    private int x;
    private int y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Point(int x,int y) {
        this.x = x;
        this.y = y;
    }

    public Point() {
    }
}
List<Point> points = new ArrayList<Point>();
points.add(new Point(1,2));
points.add(new Point(60,50));
points.add(new Point(50,3));
Collections.sort(points,new Comparator<Point>() {

public int compare(Point o1,Point o2) {
    return Integer.compare(o1.getX(),o2.getX());
}
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>