Java Comparator.comparing无法比较? [英] Java Comparator.comparing not comparing?

查看:205
本文介绍了Java Comparator.comparing无法比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跟随此问题关于按另一个列表对列表进行排序,我试图做同样的事情-但由于某种原因,它对我不起作用.我想念什么?

Following this question about sorting a list by another list, I tried to do the same thing - but from some reason it doesn't work for me. What am I missing?

    List<Double> nums = Arrays.asList(5.0, 0.9, 10.4);
    List<Double> order = Arrays.asList(3.0, 1.0, 2.0);
    nums.sort(Comparator.comparing(order::indexOf));
    System.out.println(nums);

    OUTPUT: [5.0, 0.9, 10.4]

应为[0.9、10.4、5.0](根据order).我在做什么不对?

It should be [0.9, 10.4, 5.0] (according to order). What am I not doing right?

编辑:正如您大多数人所注意到的那样,我得到了我所链接到的所有错误问题的答案. 这是我真正想要做的.

As most of you noticed, I got answer to the question I linked to all wrong. Here's what I actually want to do.

推荐答案

您正在按数字在order列表中的位置进行排序,但是这些数字在顺序列表中均不出现.在这种情况下, indexOf将为所有内容返回-1 ,这意味着所有内容都等于其他内容.在这种情况下,最终排序顺序是不确定的-尽管您可以现实地假设它不会改变.

You are sorting the numbers by their position in the order list, but none of the numbers occur in the order list. In this case, indexOf will return -1 for everything, meaning everything is equal to everything else. In such a case, the resulting sort order is unspecified - though you may realistically assume that it would not change.

这篇关于Java Comparator.comparing无法比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆