代码之家  ›  专栏  ›  技术社区  ›  onCC

如何编写比较多个参数的比较器[[副本]

  •  0
  • onCC  · 技术社区  · 7 年前

    public class Coordinate {
    
        private int x, y;
    
        public Coordinate(int x, int y) {
            this.x = x;
            this.y = 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;
        }
    
    }
    

    现在我想让比较器比较类坐标的两个实例的x和y值。举个例子:
    我有一个坐标c1,它有x=42和y=23。我的第二个坐标c2有x=23和y=54。现在我把它们都放在一个数组列表中,我想对列表进行排序。我想按以下方式分类:
    y值最小的坐标总是第一个,当有两个y值相同的坐标时,x值较低的坐标总是第一个。

    c1 (y = 4, x = 5 ) < c2 (y = 4, x = 6) < c3 (y = 5, x = 2)  
    

    那么我怎样才能写一个比较器来达到这个目的呢?
    非常感谢你!

    2 回复  |  直到 7 年前
        1
  •  3
  •   Andrew    7 年前
    Comparator<Coordinate> c = Comparator.comparingInt(Coordinate::getY)
                                         .thenComparingInt(Coordinate::getX);
    

    thenComparing 和 thenComparingX .

    var list = List.of(
            new Coordinate(6, 4),
            new Coordinate(2, 5),
            new Coordinate(5, 4)
    );
    
    list.sort(c);
    System.out.println(list);
    

    片段打印出来

    [{y=4, x=5}, {y=4, x=6}, {y=5, x=2}]
    
        2
  •  1
  •   shirish    7 年前


    import java.util.ArrayList;
    import java.util.Comparator;
    
    class Coordinate {
        private int x, y;
    
        public Coordinate(int x, int y) {
            this.x = x;
            this.y = 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 String toString() {
            return "x = " + x + " y = " + y;
        }
    }
    
    public class Temp {
        public static void main(String[] args) {
            ArrayList<Coordinate> A = new ArrayList<>();
            A.add(new Coordinate(1, 2));
            A.add(new Coordinate(2, 1));
            A.add(new Coordinate(3, 2));
            A.sort(new Comparator<Coordinate>() {
    
                @Override
                public int compare(Coordinate o1, Coordinate o2) {
                    if (o1.getY() < o2.getY()) {
                        return -1;
                    } else if (o1.getY() > o2.getY()) {
                        return 1;
                    } else {
                        if (o1.getX() < o2.getX()) {
                            return -1;
                        } else if (o1.getX() > o2.getX()) {
                            return 1;
                        }
                        return 0;
                    }
                }
            });
            System.out.println(A.toString());
        }
    }
    
    • 使用可比接口


    import java.util.ArrayList;
    
    class Coordinate implements Comparable<Coordinate> { # Notice implementing Comparable interface
        private int x, y;
    
        public Coordinate(int x, int y) {
            this.x = x;
            this.y = 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;
        }
    
        @Override
        public int compareTo(Coordinate o) { # implementing the abstract method of Comparable interface
            if (y < o.y) {
                return -1; 
            } else if (y > o.y) {
                return 1;
            } else {
                if (x < o.x) {
                    return -1;
                } else if (x > o.x) {
                    return 1;
                }
                return 0;
            }
        }
    
        public String toString() {
            return "x = " + x + " y = " + y;
        }
    }
    
    public class Temp {
        public static void main(String[] args) {
            ArrayList<Coordinate> A = new ArrayList<>();
            A.add(new Coordinate(1, 2));
            A.add(new Coordinate(2, 1));
            A.add(new Coordinate(3, 2));
            A.sort(null);
            System.out.println(A.toString());
        }
    }
    


    输出

    [x = 2 y = 1, x = 1 y = 2, x = 3 y = 2]