Java 8 Comparator comparing doesn't chain

前端 未结 2 1419
时光说笑
时光说笑 2020-12-11 02:24

Let\'s say I have a Pair class

public class Pair {
    public P p;
    public Q q;


    public Pair(P p, Q q) {
        this.p = p;
        this         


        
2条回答
  •  星月不相逢
    2020-12-11 03:07

    It should be:

    pairList.sort(Comparator.comparing(Pair::firstValue)
                                           .thenComparing(Pair::secondValue));
    

    First type parameter refers to the type being passed to Comparator. Second type parameter refers to the type that comparator should effectively compare with.

提交回复
热议问题