java中List排序无效的问题

今天使用List的排序功能,结果发现怎么排序,都没能成功。

1 list = list.stream().sorted((pre, cur) -> Double.parseDouble(pre) - Double.parseDouble(cur) > 0 ? 0 : 1).collect(Collectors.toList());

最终发现原因,比较器的返回值为正数或负数时才有意义,为0时没法排序。改成一下代码就行

1 list = list.stream().sorted((pre, cur) -> Double.parseDouble(pre) - Double.parseDouble(cur) > 0 ? -1 : 1).collect(Collectors.toList());
posted @ 2020-04-14 15:18  光何  阅读(5034)  评论(0编辑  收藏  举报