java 字符串排序工具类 分段比较

该博客介绍了一个ComparatorUtil工具类,用于对包含字符串和连续数字的组合进行排序。通过使用Hutool库,实现了根据字符串不同部分(数字和非数字)分别比较的逻辑,确保正确排序。示例中展示了如何对一系列产品名称(如'产品-2'、'产品-11-后缀'等)进行排序,并提供了相关方法的详细说明。

字符串+连续数字  组合排序 

目前只有数字排序需求  有其他需求需要自己再完善

ComparatorUtil .java

package com.xx.common.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;

import java.math.BigDecimal;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

/**
 * 功能描述: 排序比较工具类
 *
 * @author Administrator
 * @date 2021/8/14
 */
public class ComparatorUtil {

    
    public static void main(String[] args) {
        List<String> collect = CollectionUtil.newArrayList("产品-2","产品-11-后缀","产品-001","产品002","产品13后缀","产品14后缀","产品15后缀","产品16后缀","产品17后缀","产品18后缀","产品19后缀","产品2","产品20后缀","产品-3","产品4","产品5","产品6后缀","产品7后缀","产品8后缀","产品9后缀");
        collect.sort((o1,o2)->{ //分段比较
            return compare(o1,o2);
        });
        collect.forEach(System.out::println);
    }

    static Comparator<Object> compare = Collator.getInstance(Locale.CHINA);
    /**
     * 字符串比较 不同类型分段比较   正序
     * 顺序 比较结果
     * @param o1
     * @param o2
     * @return
     */
    public static int compare(String o1,String o2){
        // ---------  把字符串 按不同类型 分割成字符串数组
        String[] split1 = joinSp(o1).split("-");
        String[] split2 = joinSp(o2).split("-");
        for (int i = 0; i < split1.length; i++) {
            if(StrUtil.isBlank(split2[i])){ //短的字符 往前放
                return -1;
            }
            int result = 0;
            // ------------------------- 排序逻辑
            if(split1[i].matches("\\d*")&&split2[i].matches("\\d*")){ //数字比较
                result= (StrUtil.isBlank(split1[i])?new BigDecimal(0):new BigDecimal(split1[i])).compareTo(new BigDecimal(split2[i]));
            }else{ //字符比较
                result=compare.compare(split1[i],split2[i]);
            }
            if(result!=0){ //如果不是0  返回结果
                return result;
            }
        }
        return compare.compare(o1,o2);
    }

    /**
     * 分割不同字符类型加上-
      TODO 待优化 是否能用正则实现
     * @param str
     * @return
     */
    public static String joinSp(String str){
        ArrayList<String> strArr = CollUtil.newArrayList(str.split(""));
        int strType = 0;
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i<strArr.size();i++) {
            String next = strArr.get(i);
            int tempType = 0;
            if(next.matches("\\d")){ //数字
                tempType=1;
            }else if(next.matches("\\w")){ //字符
                tempType=2;
            }
            if(tempType!=strType&&i!=0&&!strArr.get(i-1).equals("-")){ //如果类型发生改变 并且 上一个字符不是- 就加上-
                sb.append("-");
            }
            sb.append(next);
            strType=tempType;
        }
        return sb.toString();
    }
}

maven hutool

 <dependency>
  <groupId>cn.hutool</groupId>
  <artifactId>hutool-core</artifactId>
  <version>5.7.2</version>
  <scope>compile</scope>
</dependency>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值