使用JAVA自带的Collections.sort给集合泛型进行中英文排序

1、前提介绍:

进场会遇到集合泛型内的元素需要排序,但是元素中存在,中文 英文 数字,给各种类型进行排序,那就没有必要了,但是使用jdk自带的Collections.sort进行排序

1.1、构造User类

public class User {
    private String gender;//性别
    private String name;//姓名

    public User(String gender, String name) {
        this.gender = gender;
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

1.2、构造StringUtil工具类进行排序

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

public class StringUtil {
    private static Collator collator;

    //对list泛型进行排序
    public static List<User>  initUserListNameCompare(List<User> userList){
        //使用中文排序
        collator = Collator.getInstance(Locale.CHINA);
        Collections.sort(userList, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                //针对User的name属性进行排序
                return collator.compare(o1.getName().toString(), o2.getName().toString());
            }
        });
        return userList;
    }
}

1.3、测试代码

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("男","张三"));
        userList.add(new User("女","李四"));
        userList.add(new User("女","赵六"));
        userList.add(new User("男","王五"));
        userList.add(new User("女","小明"));
        userList.add(new User("女","z"));
        userList.add(new User("女","c"));
        userList.add(new User("女","b"));
        userList.add(new User("男","x"));
        userList.add(new User("女","a"));
        userList.add(new User("女","11111"));
        userList.add(new User("男","12"));
        userList.add(new User("男","23"));
        userList.add(new User("男","22222"));
        userList.add(new User("男","666666"));
        userList.add(new User("男","6"));
        userList.add(new User("男","999"));
        userList.add(new User("男","8"));

        //姓名排序前
        System.out.println("姓名排序前");
        for (int i = 0;i<userList.size();i++){
            System.out.println(userList.get(i).toString());
        }

        StringUtil.initUserListNameCompare(userList);
        //姓名排序后
        System.out.println("姓名排序后");
        for (int i = 0;i<userList.size();i++){
            System.out.println(userList.get(i).toString());
        }
    }
}

1.4、编译输出

针对name属性进行排序,查看排序前后的变化
数字排在最前,英文字母其次,汉字则按照拼音进行排序。

姓名排序前
User{gender='男', name='张三'}
User{gender='女', name='李四'}
User{gender='女', name='赵六'}
User{gender='男', name='王五'}
User{gender='女', name='小明'}
User{gender='女', name='z'}
User{gender='女', name='c'}
User{gender='女', name='b'}
User{gender='男', name='x'}
User{gender='女', name='a'}
User{gender='女', name='11111'}
User{gender='男', name='12'}
User{gender='男', name='23'}
User{gender='男', name='22222'}
User{gender='男', name='666666'}
User{gender='男', name='6'}
User{gender='男', name='999'}
User{gender='男', name='8'}
姓名排序后
User{gender='女', name='11111'}
User{gender='男', name='12'}
User{gender='男', name='22222'}
User{gender='男', name='23'}
User{gender='男', name='6'}
User{gender='男', name='666666'}
User{gender='男', name='8'}
User{gender='男', name='999'}
User{gender='女', name='a'}
User{gender='女', name='b'}
User{gender='女', name='c'}
User{gender='男', name='x'}
User{gender='女', name='z'}
User{gender='女', name='李四'}
User{gender='男', name='王五'}
User{gender='女', name='小明'}
User{gender='男', name='张三'}
User{gender='女', name='赵六'}


你可能感兴趣的:(java,开发语言,排序算法,算法)