c# - 在 C# 中按字母数字顺序对任意属性的通用列表进行排序

标签 c# .net list sorting alphanumeric

是否可以在 C# 中按字母数字对任意属性的通用列表进行排序?如果问题不清楚,请告诉我,我会举个例子。

提前致谢

注意:我发现此链接可以执行此操作,但不是字母数字方式。谁能帮我吗? http://blog.codewrench.net/2009/04/14/sorting-a-generic-list-on-arbitrary-property/

最佳答案

这里是一个快速的字母数字排序(也可以用于其他带有数字的排序)。

C# 字母数字排序 http://www.dotnetperls.com/alphanumeric-sorting

public class AlphanumComparatorFast : IComparer
{
    public int Compare(object x, object y)
    {
    string s1 = x as string;
    if (s1 == null)
    {
        return 0;
    }
    string s2 = y as string;
    if (s2 == null)
    {
        return 0;
    }

    int len1 = s1.Length;
    int len2 = s2.Length;
    int marker1 = 0;
    int marker2 = 0;

    // Walk through two the strings with two markers.
    while (marker1 < len1 && marker2 < len2)
    {
        char ch1 = s1[marker1];
        char ch2 = s2[marker2];

        // Some buffers we can build up characters in for each chunk.
        char[] space1 = new char[len1];
        int loc1 = 0;
        char[] space2 = new char[len2];
        int loc2 = 0;

        // Walk through all following characters that are digits or
        // characters in BOTH strings starting at the appropriate marker.
        // Collect char arrays.
        do
        {
        space1[loc1++] = ch1;
        marker1++;

        if (marker1 < len1)
        {
            ch1 = s1[marker1];
        }
        else
        {
            break;
        }
        } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));

        do
        {
        space2[loc2++] = ch2;
        marker2++;

        if (marker2 < len2)
        {
            ch2 = s2[marker2];
        }
        else
        {
            break;
        }
        } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));

        // If we have collected numbers, compare them numerically.
        // Otherwise, if we have strings, compare them alphabetically.
        string str1 = new string(space1);
        string str2 = new string(space2);

        int result;

        if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
        {
        int thisNumericChunk = int.Parse(str1);
        int thatNumericChunk = int.Parse(str2);
        result = thisNumericChunk.CompareTo(thatNumericChunk);
        }
        else
        {
        result = str1.CompareTo(str2);
        }

        if (result != 0)
        {
        return result;
        }
    }
    return len1 - len2;
    }
}

用法

var unordered = new[] { "100F", "50F", "SR100", "SR9" };
var ordered = unordered.OrderBy(s => s, new AlphanumComparatorFast());

这是一篇关于这个问题的好文章:

人类排序:自然排序顺序 http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html

关于c# - 在 C# 中按字母数字顺序对任意属性的通用列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8362208/

相关文章:

c# - C#中通过串口读取整个字符串

c# - 如何使用修剪功能修剪列表内部

c# - 在 C# 中拆分字符串中的子字符串

c# - 在另一个线程中使用线程时发生内存泄漏

c# - 如何使用 C# 进行 Excel 文件的映射

python - ValueError : list. remove(x) : x not in list, 但我没有看到错误

python - 如何在Python中比较两个列表中各自列表中相同索引中的元素?

C# Web 浏览器控件未正确更新

c# - IL,发出默认构造函数调用

c# - 从 AJAX 发布的字符串中删除 BOM 字符