我有列表{"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));
答案 0 :(得分:5)
列表值按字典顺序排序。您可以转换值
list.stream().sorted((s1, s2)-> new Integer(s1).compareTo(new Integer(s2))).forEach(System.out::println);