字符串排序,集合按对象属性排序,带字符串数字的中文排序

按中文排序不好使,自己写比较器
对象集合list,
按 teachWeek 排序

//teachWeek是实体类属性名
Collections.sort(list,new StrComparatorUtils("teachWeek"));
 return  list;

teachWeek 的值类型

创建比较器

/**
 * @author xia
 * @date 2021/11/25 
 */
public class StrComparatorUtils<T> implements Comparator<T> {
    private String str1, str2;
    private int pos1, pos2, len1, len2;
    private String propertyName;
    private boolean isDesc = false;

    /**
     * 传入对象的属性名,根据该属性值进行排序
     * @param propertyName
     */
    public StrComparatorUtils(String propertyName) {
        this.propertyName = propertyName;
    }

    /**
     * 传入对象的属性名,根据该属性值进行排序
     * @param propertyName
     * @param isDesc 是否倒序,默认为false
     */
    public StrComparatorUtils(String propertyName, boolean isDesc) {
        this.propertyName = propertyName;
        this.isDesc = isDesc;
    }

    @Override
    public int compare(T o1, T o2) {
        if (isDesc) {
            str2 = getPropertyValue(o1);
            str1 = getPropertyValue(o2);
        } else {
            str1 = getPropertyValue(o1);
            str2 = getPropertyValue(o2);
        }
        len1 = str1.length();
        len2 = str2.length();
        pos1 = pos2 = 0;

        int result = 0;
        while (result == 0 && pos1 < len1 && pos2 < len2) {
            char ch1 = str1.charAt(pos1);
            char ch2 = str2.charAt(pos2);

            if (Character.isDigit(ch1)) {
                result = Character.isDigit(ch2) ? compareNumbers() : -1;
            } else if (Character.isLetter(ch1)) {
                result = Character.isLetter(ch2) ? compareOther(true) : 1;
            } else {
                result = Character.isDigit(ch2) ? 1
                        : Character.isLetter(ch2) ? -1
                        : compareOther(false);
            }
            pos1++;
            pos2++;
        }

        return result == 0 ? len1 - len2 : result;
    }

    private String getPropertyValue(T o1) {
        String str = "";
        try {
            Field field = o1.getClass().getDeclaredField(propertyName);
            field.setAccessible(true);
            Object obj = field.get(o1);
            if (obj != null){
                return obj.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    private int compareNumbers() {
        int end1 = pos1 + 1;
        while (end1 < len1 && Character.isDigit(str1.charAt(end1))) {
            end1++;
        }
        int fullLen1 = end1 - pos1;
        while (pos1 < end1 && str1.charAt(pos1) == '0') {
            pos1++;
        }

        int end2 = pos2 + 1;
        while (end2 < len2 && Character.isDigit(str2.charAt(end2))) {
            end2++;
        }
        int fullLen2 = end2 - pos2;
        while (pos2 < end2 && str2.charAt(pos2) == '0') {
            pos2++;
        }

        int delta = (end1 - pos1) - (end2 - pos2);
        if (delta != 0) {
            return delta;
        }

        while (pos1 < end1 && pos2 < end2) {
            delta = str1.charAt(pos1++) - str2.charAt(pos2++);
            if (delta != 0) {
                return delta;
            }
        }

        pos1--;
        pos2--;

        return fullLen2 - fullLen1;
    }

    private int compareOther(boolean isLetters) {
        char ch1 = str1.charAt(pos1);
        char ch2 = str2.charAt(pos2);

        if (ch1 == ch2) {
            return 0;
        }

        if (isLetters) {
            ch1 = Character.toUpperCase(ch1);
            ch2 = Character.toUpperCase(ch2);
            if (ch1 != ch2) {
                ch1 = Character.toLowerCase(ch1);
                ch2 = Character.toLowerCase(ch2);
            }
        }
        return ch1 - ch2;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏路加

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值