Java Stream.sorted()给出了错误的结果

时间:2015-10-29 20:43:09

标签: java java-8 java-stream

我有列表{"100","5","1","300","7"},当我将stream().sorted()用于此列表时,它会给我以下结果{"1","100","300","5","7"}。看起来它只按第一个数字排序。怎么了?

ArrayList<String> list = new ArrayList<>(Arrays.asList(
            "100","5","1","300","7"
));

list.stream().sorted().forEach(e -> System.out.println(e));

1 个答案:

答案 0 :(得分:5)

列表值按字典顺​​序排序。您可以转换值

list.stream().sorted((s1, s2)-> new Integer(s1).compareTo(new Integer(s2))).forEach(System.out::println);