实际业务场景可能存在需要根据对象中文名称来进行排序的情况,下面分享下实现方案:
话不多说,上代码
List<RepairVo> list = new ArrayList<>();
list.add(new RepairVo(1,"赵某"));
list.add(new RepairVo(2,"吴某"));
list.add(new RepairVo(3,"安某"));
list.add(new RepairVo(4,"包某"));
System.out.println(ArrayUtil.toString(list));
//输出结果:
[RepairVo(id=1, repairName=赵某), RepairVo(id=2, repairName=吴某), RepairVo(id=3, repairName=安某), RepairVo(id=4, repairName=包某)]
//这里使用到Comparator 和 lambda语法 实现首字母排序
Comparator comparator = Collator.getInstance(Locale.CHINA);
list = list.stream().sorted((p1,p2) -> {
return comparator.compare(p1.getRepairName(),p2.getRepairName());
}).collect(Collectors.toList());
System.out.println(ArrayUtil.toString(list));
//输出结果:
[RepairVo(id=1, repairName=安某), RepairVo(id=44, repairName=包某), RepairVo(id=2, repairName=吴某), RepairVo(id=1, repairName=赵某)]