Java List集合 实体类按汉字拼音排序

本文介绍两种方式,方式一比较简便,只需要实现Comparable接口,复写compareto方法,方法二需要添加pinyin4j-2.5.0.jar包

Java List集合 实体类按汉字拼音排序   方式一

java中List是有序的,集合的元素是按照添加顺序排序的,但在实际开发过程中,有可能需要对其内容进行排序,下面是list集合 元素实体类按照汉字拼音排序

首先需要排序的对象需要实现Comparable接口并且并复写compareto方法:

package demo;

public class User implements Comparable<User>{
    private String name; //姓名

    private int age; // 年龄
    
    
    public User(String name, int age) {
        this.name = name;
        this.age =age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    public String toString() {
        return "[name=" + name + ", age=" + age + "]";
    }
    
    
    //复写compareto方法
    public int compareTo(User name) {
        // TODO 自动生成的方法存根
        return this.name.compareTo(name.getName());
    }

}

调用方法,对集合中的实体类按汉字首字母排序

 public static void main(String[] args) throws UnsupportedEncodingException {
    

    List<User> list = new ArrayList<User>();
    list.add(new User("张三", 5));
    list.add(new User("张三", 6));
    list.add(new User("张三", 7));
    list.add(new User("李四", 30));
    list.add(new User("安安", 30));
    list.add(new User("王五", 19));
    list.add(new User("陈十七", 17));
    list.add(new User("弟弟", 17));
    list.add(new User("陈十七", 10));
  System.out.println(list.toString());
  //Collections工具类的sort()方法对list集合元素排序   Collections.sort(list,
new Comparator<User>() {   public int compare(User info1, User info2) {     //获取中文环境   Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);   return com.compare(info1.getName(), info2.getName());   }   });   System.out.println(list); }

结果打印:

Java List集合 实体类按汉字拼音排序   方式二

需要添加jar包:pinyin4j-2.5.0.jar

下载地址:http://sourceforge.net/projects/pinyin4j/

代码:

        List<User> list = new ArrayList<User>();
        list.add(new User("张三", 5));
        list.add(new User("张三", 6));
        list.add(new User("张三", 7));
        list.add(new User("李四", 30));
        list.add(new User("安安", 30));
        list.add(new User("王五", 19));
        list.add(new User("陈十七", 17));
        list.add(new User("弟弟", 17));
        list.add(new User("陈十七", 10));
    Collections.sort(list,new Comparator<User>() {
        public int compare(User s1, User s2) {
            String o1 = s1.getName();
            String o2 = s2.getName();
            for (int i = 0; i < o1.length() && i < o2.length(); i++) {
     
            int codePoint1 = o1.charAt(i);
            int codePoint2 = o2.charAt(i);
     
            if (Character.isSupplementaryCodePoint(codePoint1)
                || Character.isSupplementaryCodePoint(codePoint2)) {
            i++;
            }
     
            if (codePoint1 != codePoint2) {
            if (Character.isSupplementaryCodePoint(codePoint1)
                || Character.isSupplementaryCodePoint(codePoint2)) {
                return codePoint1 - codePoint2;
            }
     
            String pinyin1 = PinyinHelper.toHanyuPinyinStringArray((char) codePoint1) == null 
                    ? null : PinyinHelper.toHanyuPinyinStringArray((char) codePoint1)[0];
            String pinyin2 = PinyinHelper.toHanyuPinyinStringArray((char) codePoint2) == null 
                    ? null : PinyinHelper.toHanyuPinyinStringArray((char) codePoint2)[0];
     
            if (pinyin1 != null && pinyin2 != null) { // 两个字符都是汉字
                if (!pinyin1.equals(pinyin2)) {
                return pinyin1.compareTo(pinyin2);
                }
            } else {
                return codePoint1 - codePoint2;
            }
            }
        }
        return o1.length() - o2.length();
        }
    }); // 按年龄排序
    
    
    System.out.println(list.toString());
原文地址:https://www.cnblogs.com/ttqi/p/14035741.html