我想使用比较器对 String 类型的 ArrayList 进行排序。我只找到了有关如何在 ArrayList 存储对象时执行此操作的示例。
我有一个包含 10 个符号的字符串 ArrayList,其中最后 5 个符号是组成数字的数字。我想按每个字符串末尾的数字的升序求解数组列表。我该怎么做?
谢谢!
最佳答案
这是完成任务的一种方式; sorted
接受一个 Comparator
对象。
List<String> result = myArrayList.stream().sorted(Comparator.comparingInt(e -> Integer.parseInt(e.substring(5))))
.collect(Collectors.toList());
或者简单地说:
myArrayList.sort(Comparator.comparingInt(e -> Integer.parseInt(e.substring(5))));
关于Java:ArrayList<String> 使用比较器排序(ArrayLists 不存储对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46991966/