基于,实现Comparator或ToIntFunction,Java 8的流排序,反向绝对值

我正在与

someList.sorted(Comparator.comparingInt(someClass::getValue).reversed())

Now, I need to sorting by Absolute Value like this answer: abs_number = (number < 0) ? -number : number; (Not using Math.abs(x) or Math.sqrt(Math.pow(x, 2)))

How implement a new java.util.Comparator or java.util.function.ToIntFunction in order by AbsoluteValue in reverse Order?

评论
波哩瓶
波哩瓶

Check this Question:

someList.sorted((a, b) -> 
    Integer.valueOf(b.getValue() < 0 ? -b.getValue() : b.getValue())
    .compareTo(a.getValue() < 0 ? -a.getValue() : a.getValue())
)
点赞
评论