c# 数字字母混合排序比较器

项目上涉及到一些编码的排序,网上找了一下没有合适的比较器,于是自己动手编写了一个。供大家学习参考。

 class StringComparer : IComparer<string>
        {
            public bool MatchCase { get; }
            public StringComparer(bool matchCase) { MatchCase = matchCase; }
            private int CharCompare(char a, char b, bool matchCase)
            {
                char _a = char.MinValue, _b = char.MinValue;
                if (matchCase) { _a = a; _b = b; }
                else { _a = char.ToUpper(a); _b = char.ToUpper(b); }
                if (_a > _b) return 1;
                if (_a < _b) return -1;
                return 0;
            }
            public int Compare(string x, string y)
            {
                int len;
                if (x.Length > y.Length) len = x.Length;
                else len = y.Length;
                string numericx = "";
                string numericy = "";
                for (int i = 0; i < len; i++)
                {
                    char cx = char.MinValue;
                    char cy = char.MinValue;
                    if (i < x.Length) cx = x[i];
                    if (i < y.Length) cy = y[i];
                    if (cx >= 48 && cx <= 57) numericx += cx;
                    if (cy >= 48 && cy <= 57) numericy += cy;
                    if (i == len - 1)
                    {
                        if (numericx.Length > 0 && numericy.Length > 0)
                        {
                            if (decimal.Parse(numericx) < decimal.Parse(numericy)) return -1;
                            if (decimal.Parse(numericx) > decimal.Parse(numericy)) return 1;
                        }
                        return CharCompare(cy, cy, MatchCase);
                    }
                    if ((cx >= 48 && cx <= 57) && (cy >= 48 && cy <= 57)) continue;
                    if (numericx.Length > 0 && numericy.Length > 0)
                    {
                        if (decimal.Parse(numericx) < decimal.Parse(numericy)) return -1;
                        if (decimal.Parse(numericx) > decimal.Parse(numericy)) return 1;
                    }
                    if (CharCompare(cx, cy, MatchCase) == 0) continue;
                    return CharCompare(cx, cy, MatchCase);
                }
                return 0;
            }
        }

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中排序字母数字混合的问题,可以通过自定义排序规则和使用Java的排序算法来实现。 首先,我们可以定义一个实现Comparator接口的自定义比较器。这个比较器可以判断两个字符的类型并进行比较。如果两个字符的类型相同,我们可以直接使用String类提供的compareTo方法进行比较;如果两个字符的类型不同,我们可以设定一个优先级,例如将字母排在数字之前。可以使用Character类的isLetter和isDigit方法来判断字符的类型。 然后,我们可以使用Collections类的sort方法来排序一个包含字母数字混合的列表。将自定义比较器作为参数传入sort方法即可完成排序。 下面是一个示例代码: import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("A"); list.add("1"); list.add("C"); list.add("3"); list.add("B"); list.add("2"); // 使用自定义比较器进行排序 Collections.sort(list, new CustomComparator()); // 打印排序结果 for (String s : list) { System.out.println(s); } } static class CustomComparator implements Comparator<String> { @Override public int compare(String s1, String s2) { boolean isLetter1 = Character.isLetter(s1.charAt(0)); boolean isLetter2 = Character.isLetter(s2.charAt(0)); if (isLetter1 && isLetter2) { return s1.compareTo(s2); } else if (!isLetter1 && isLetter2) { return 1; } else if (isLetter1 && !isLetter2) { return -1; } else { return s1.compareTo(s2); } } } } 通过以上代码,我们可以将字母数字混合的列表按照字母数字的顺序进行排序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值