ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#中的字母数字排序

2019-06-29 23:54:44  阅读:769  来源: 互联网

标签:c algorithm sorting alphanumeric


我有这样的CSV数据:

数;版本
1; AA.1
1; A01.1
1; A01.2
1; Z.7

在这里,我需要按如下方式对Version列进行排序:

数;版本
1; AA.1
1; Z.7
1; A01.2
1; A01.1

所以如果你看到,Z.7条目应该在AA.1之后.基本上降序排序应该像:

分类版本:

BB
AA
ž
C

一个

我已经试过了
也在http://www.DaveKoelle.com讨论了Alphanum算法
自然排序比较器
http://www.codeproject.com/Articles/22517/Natural-Sort-Comparer.除了上面提到的排序,它做了我想要的一切.

解决方法:

因此,看起来您需要对CSV文件中的一些自定义逻辑进行排序.要执行此操作,您必须实现IComparer< string>在一个类上,根据您的要求实现Compare方法.看起来在你的情况下,你需要首先将字符串分成两部分,字母表部分和数字部分(使用正则表达式),然后首先比较字符串,如果两者相同则比较数字部分并相应地返回值.

然后,您可以使用此Comparer类对它们进行排序.

更新

代码示例

public class CustomStringComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        int? result;
        if (AnyParamIsNull(x, y, out result))
        {
            return result.Value;
        }

        string x1, y1;
        double x2, y2;
        SplitInput(x, out x1, out x2);
        SplitInput(y, out y1, out y2);
        if (x1.Length == y1.Length)
        {
            result = string.Compare(x1, y1);
            if (result.Value == 0)
            {

                if (x2 < y2)
                {
                    return -1;
                }
                else if (x2 > y2)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }


            }
            else
            {
                return result.Value;
            }
        }
        else
        {
            //To make AA before Z when desending
            return x1.Length - y1.Length;
        }

    }

    private bool SplitInput(string input, out string alphabets, out double number)
    {
        Regex regex = new Regex(@"\d*[.]?\d+");
        Match match = regex.Match(input);
        if (match.Success)
        {
            number = double.Parse(match.Value, CultureInfo.InvariantCulture);
            alphabets = input.Replace(match.Value, "");
            return true;
        }
        else
        {
            throw new ArgumentException("Input string is not of correct format", input);
        }
    }


    private bool AnyParamIsNull(string x, string y, out int? result)
    {
        result = null;
        if (x == null)
        {
            if (y == null)
            {
                // If x is null and y is null, they're 
                // equal.  
                result = 0;
                return true;
            }
            else
            {
                // If x is null and y is not null, y 
                // is greater.  
                result = -1;
                return true;
            }
        }
        else
        {
            // If x is not null... 
            // 
            if (y == null)
            // ...and y is null, x is greater.
            {
                result = 1;
                return true;
            }
        }
        return false;
    }
}

使用此Comparer

        List<string> input = new List<string>()
        {
            "AA.1",
            "A01.1",
            "A01.2",
            "Z.7"
        };

        input.Sort(new CustomStringComparer());
        //To sort decending
        input.Reverse();

注意:我已经证明上面的代码是正确的,否则没有.可能存在一些可能无法按预期工作的边缘情况

标签:c,algorithm,sorting,alphanumeric
来源: https://codeday.me/bug/20190629/1331051.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有