Java 按比较器给出错误的集合排序

Java collection sort by comparator giving error

我尝试使用集合对 Java ArrayList 进行排序,但它不起作用,即使我尝试遵循其他 .

的代码

我有一个名为 PcdPoint 的 class,它具有方法 getScore() 和 getType(),其中 getScore() returns 是一个双精度数,getType() returns 是一个整数。 (不是原子)

下面的代码应该可以工作,但它给了我一个错误:“无法推断类型

Collections.sort(pointList,
            Comparator.comparing((PcdPoint a, PcdPoint b) -> a.getScore() - b.getScore())
            .thenComparing((PcdPoint a, PcdPoint b) -> a.getType() - b.getType()));

所以我尝试查找文档并尝试这样做

Collections.sort(pointList,
            Comparator<PcdPoint>.comparing((a, b) -> a.getScore() - b.getScore())
            .thenComparing((a, b) -> a.getType() - b.getType()));

Collections.sort(pointList,
            Comparator.comparing((a, b) -> a.getScore() - b.getScore())
            .thenComparing((a, b) -> a.getType() - b.getType()));

但这两个似乎都不起作用。

如果您想先按分数排序,然后再按类型排序,您的比较器应该如下所示。

Collections.sort(pointList, 
    Comparator.comparingDouble(PcdPoint::getScore)
        .thenComparingInt(PcdPoint::getType));