【Java基础·Comparator多字段排序】

姬浩渺
2023-12-01

【Java基础·Comparator多字段排序】

需求

对指定List按照 身份证号、姓名、入学时间倒序排列

版本1

package com.biaogexf.tools;

import lombok.Data;

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

public class MultiFieldSortByComparatorTest {
    public static void main(String[] args) {
        List<User> usersList = new ArrayList<>();

        // 构建多个实例
        fillContent(usersList);
        // 输出列表
        printData(usersList);
        // 按照 身份证号、姓名、入学时间 倒序
        dataSort(usersList);
        // 查看排序后的列表
        printData(usersList);
    }

    private static void dataSort(List<User> usersList) {
        Collections.sort(usersList, new Comparator<User>(){
            @Override
            public int compare(User obj1, User obj2) {
                int cr = 0;
                // 按身份证号降序排列
                int a = obj2.getIdNumber().compareTo(obj1.getIdNumber());
                if (a != 0) {
                    cr = (a > 0) ? 3: -1;
                } else {
                    // 按姓名降序排列
                    a = obj2.getName().compareTo(obj1.getName());
                    if (a != 0) {
                        cr = (a > 0) ? 2: -2;
                    } else {
                        // 按入学时间降序排列
                        a = obj2.getInfoDate().compareTo(obj1.getInfoDate());
                        if (a != 0) {
                            cr = (a > 0) ? 1: -3;
                        }
                    }
                }

                return cr;
            }
        });
    }

    private static void printData(List<User> usersList) {
        System.out.println("====================");
        for (User user : usersList) {
            System.out.println(user);
        }
        System.out.println("====================");
    }

    private static void fillContent(List<User> usersList) {
        User user1 = new User();
        user1.setIdNumber("1");
        user1.setName("11");
        user1.setInfoDate("111");
        usersList.add(user1);

        User user2 = new User();
        user2.setIdNumber("2");
        user2.setName("11");
        user2.setInfoDate("111");
        usersList.add(user2);

        User user3 = new User();
        user3.setIdNumber("2");
        user3.setName("22");
        user3.setInfoDate("222");
        usersList.add(user3);

        User user5 = new User();
        user5.setIdNumber("2");
        user5.setName("11");
        user5.setInfoDate("222");
        usersList.add(user5);
    }
}

@Data
class User {
    /** 身份证号 */
    private String idNumber;
    /** 姓名 */
    private String name;
    /** 入学时间 */
    private String infoDate;
}

阿里巴巴规范:

1.Collections.sort可替换为List.sort

2.匿名Comparator()可替换为lambda表达式

版本2

package com.biaogexf.tools;

import lombok.Data;

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

public class MultiFieldSortByComparatorTest {
    public static void main(String[] args) {
        List<User> usersList = new ArrayList<>();

        // 构建多个实例
        fillContent(usersList);
        // 输出列表
        printData(usersList);
        // 按照 身份证号、姓名、入学时间 倒序
        dataSort(usersList);
        // 查看排序后的列表
        printData(usersList);
    }

    private static void dataSort(List<User> usersList) {
        usersList.sort((obj1, obj2) -> {
            int cr = 0;
            // 按身份证号降序排列
            int a = obj2.getIdNumber().compareTo(obj1.getIdNumber());
            if (a != 0) {
                cr = (a > 0) ? 3 : -1;
            } else {
                // 按姓名降序排列
                a = obj2.getName().compareTo(obj1.getName());
                if (a != 0) {
                    cr = (a > 0) ? 2 : -2;
                } else {
                    // 按入学时间降序排列
                    a = obj2.getInfoDate().compareTo(obj1.getInfoDate());
                    if (a != 0) {
                        cr = (a > 0) ? 1 : -3;
                    }
                }
            }

            return cr;
        });
    }

    private static void printData(List<User> usersList) {
        System.out.println("====================");
        for (User user : usersList) {
            System.out.println(user);
        }
        System.out.println("====================");
    }

    private static void fillContent(List<User> usersList) {
        User user1 = new User();
        user1.setIdNumber("1");
        user1.setName("11");
        user1.setInfoDate("111");
        usersList.add(user1);

        User user2 = new User();
        user2.setIdNumber("2");
        user2.setName("11");
        user2.setInfoDate("111");
        usersList.add(user2);

        User user3 = new User();
        user3.setIdNumber("2");
        user3.setName("22");
        user3.setInfoDate("222");
        usersList.add(user3);

        User user5 = new User();
        user5.setIdNumber("2");
        user5.setName("11");
        user5.setInfoDate("222");
        usersList.add(user5);
    }
}

@Data
class User {
    /** 身份证号 */
    private String idNumber;
    /** 姓名 */
    private String name;
    /** 入学时间 */
    private String infoDate;
}

输出

====================
User(idNumber=1, name=11, infoDate=111)
User(idNumber=2, name=11, infoDate=111)
User(idNumber=2, name=22, infoDate=222)
User(idNumber=2, name=11, infoDate=222)
====================
====================
User(idNumber=2, name=22, infoDate=222)
User(idNumber=2, name=11, infoDate=222)
User(idNumber=2, name=11, infoDate=111)
User(idNumber=1, name=11, infoDate=111)
====================

感悟

比较器Comparator中的compare方法定义的是排序的规则!!!

 类似资料: