java实现List集合中的对象按中文首字母排序

假设有个Employee的类

public class Employee {

    private String name;
    private int age;
    private double salary;
    
}

有个employees的List集合

List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 37, 5555.99),
            new Employee("王五", 50, 6666.66),
            new Employee("赵六", 16, 3333.33),
            new Employee("田七", 8, 7777.77)
    );

按中文首字母排序实现

@Test
    public void sort(){

        // 获取中文环境
        Comparator comparator = Collator.getInstance(Locale.CHINA);
        // 排序实现
        Collections.sort(employees, (e1, e2) -> {
            return comparator.compare(e1.getName(), e2.getName());
        });

        // java8打印排序后的结果
        employees.stream().forEach(System.out::println);

    }

你可能感兴趣的:(java,中文首字母排序,java,排序,集合对象排序,首字母排序)