栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 数组自定义多条件排序

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 数组自定义多条件排序

排序规则:人员在线状态 -> 按首字符升序: 英文(不区分大小写)-> 中文-> 数字-> 特殊字符。

代码
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.vo.EmployeeVO;

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


public class SortNewMain {

    public static void main(String[] args) {
        List list = CollectionUtil.newArrayList();
        list.add(new EmployeeVO().setName("B测试").setOnLine(false));
        list.add(new EmployeeVO().setName("a测试").setOnLine(false));
        list.add(new EmployeeVO().setName("233").setOnLine(false));
        list.add(new EmployeeVO().setName("1").setOnLine(false));
        list.add(new EmployeeVO().setName("中文").setOnLine(false));
        list.add(new EmployeeVO().setName("阿布").setOnLine(false));
        list.add(new EmployeeVO().setName("^%$#").setOnLine(true));
        list.add(new EmployeeVO().setName("&").setOnLine(false));

        System.out.println("排序前:");
        System.out.println(JSONUtil.toJsonStr(list));

        // 在线状态 -> 按首字母升序,先英文(不区分大小写),然后中文,然后数字,然后特殊字符)
        list.sort(
                Comparator.comparing(EmployeeVO::getOnLine).reversed()
                        .thenComparing(new CustomComparator(RegexPool.WORD))
                        .thenComparing(new CustomComparator(RegexPool.CHINESE))
                        .thenComparing(new CustomComparator(RegexPool.NUMBERS))
        );
        System.out.println("首字母排序后:");
        System.out.println(JSONUtil.formatJsonStr(JSONUtil.toJsonStr(list)));
    }

    
    public static class CustomComparator implements Comparator {

        private final String PATTERN;

        public CustomComparator(String PATTERN) {
            this.PATTERN = PATTERN;
        }

        @Override
        public int compare(EmployeeVO o1, EmployeeVO o2) {
            String firstLetter1 = StrUtil.sub(o1.getName(), 0, 1);
            String firstLetter2 = StrUtil.sub(o2.getName(), 0, 1);
            boolean b1 = ReUtil.isMatch(PATTERN, firstLetter1);
            boolean b2 = ReUtil.isMatch(PATTERN, firstLetter2);

            // 英文
            if (StrUtil.equals(PATTERN, RegexPool.WORD) && b1 && b2) {
                return Collator.getInstance(Locale.UK).compare(firstLetter1, firstLetter2);
            }
            // 中文
            if (StrUtil.equals(PATTERN, RegexPool.CHINESE) && b1 && b2) {
                return Collator.getInstance(Locale.CHINESE).compare(firstLetter1, firstLetter2);
            }
            // 数字
            if (StrUtil.equals(PATTERN, RegexPool.NUMBERS) && b1 && b2) {
                return Collator.getInstance().compare(firstLetter1, firstLetter2);
            }
            if (b1 ^ b2) {
                return b1 ? -1 : 1;
            }
            return 0;
        }
    }

}

结果

排序前:

[
    {
        "name": "B测试",
        "onLine": false
    },
    {
        "name": "a测试",
        "onLine": false
    },
    {
        "name": "233",
        "onLine": false
    },
    {
        "name": "1",
        "onLine": false
    },
    {
        "name": "中文",
        "onLine": false
    },
    {
        "name": "阿布",
        "onLine": false
    },
    {
        "name": "^%$#",
        "onLine": true
    },
    {
        "name": "&",
        "onLine": false
    }
]

排序后:

[
    {
        "name": "^%$#",
        "onLine": true
    },
    {
        "name": "a测试",
        "onLine": false
    },
    {
        "name": "B测试",
        "onLine": false
    },
    {
        "name": "阿布",
        "onLine": false
    },
    {
        "name": "中文",
        "onLine": false
    },
    {
        "name": "1",
        "onLine": false
    },
    {
        "name": "233",
        "onLine": false
    },
    {
        "name": "&",
        "onLine": false
    }
]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1028310.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号