java_根据实体字段中的中文汉字排序

package com.xiahaolei.algorithm.test;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author xiaQ
 * @version 1.0
 * @date 2021/3/14 11:20
 */
public class Test {
    public static void main(String[] args) {
        List<TestPerson> dataList = getDataList();
//按照名称排序
        Collections.sort(dataList, new Comparator<TestPerson>() {
            @Override
            public int compare(TestPerson o1, TestPerson o2) {
                //排序规则:按照汉字拼音首字母排序
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getName(), o1.getName());
                //t2 t1 交换可以改变排序顺序
            }
        });
        System.out.println(dataList);
    }

    private static List<TestPerson> getDataList() {
        List<TestPerson> testPersonList = new ArrayList<>();
        TestPerson p = TestPerson.builder().name("王霸之气").age(11).build();
        TestPerson p1 = TestPerson.builder().name("算测").age(11).build();
        TestPerson p2 = TestPerson.builder().name("字段").age(11).build();
        testPersonList.add(p);
        testPersonList.add(p1);
        testPersonList.add(p2);
        return testPersonList;
    }
}

  

踏踏实实的走,每一步都算数
原文地址:https://www.cnblogs.com/keepstudy-xiahl/p/14532429.html