[英]Java Sorting list alpha
I'm trying to sort the list alphabetically Unit class has: name and number. 我正在尝试按字母顺序对列表进行排序单元类具有:名称和数字。 I'm trying to sort by name. 我正在尝试按名称排序。
using my code it's sorted but not alphabetically. 使用我的代码进行排序,但不按字母顺序排序。 My code is: (tried both the commented and the not commented one. 我的代码是:(尝试了已注释和未注释的代码。
List<Unit> yards = new ArrayList<>(unitsApi.getAllUnits());
Collections.sort(yards, new BeanComparator<Unit>("name"));
//another try
List<Unit> sortedNames = yards.stream().sorted().collect(Collectors.toList());
//num3 another
//List<Unit> sortedList = yards
.stream().sorted(Comparator.comparing(Object::toString))
.collect(Collectors.toList());
model.addAttribute("yards", sortedList);
Specify the sort key ie: 指定排序键,即:
List<Unit> sortedList = yards.stream()
.sorted(Comparator.comparing(e -> e.getName()))
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.