zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    

      

    踏踏实实的走,每一步都算数
  • 相关阅读:
    web.xml中/与/*的区别
    restController相关
    mvc:resources
    RequestMethod 相关
    Springside学习
    命名规范的反思
    C++ 构造中调用构造
    C++ 匿名对象的生命周期
    C++ 构造函数的对象初始化列表
    C++ 类的构造函数使用规则
  • 原文地址:https://www.cnblogs.com/keepstudy-xiahl/p/14532429.html
Copyright © 2011-2022 走看看