在C#中排序上的任意性泛型列表字母数字 [英] Sorting a generic list on arbitrary property alphanumerically in c#

查看:121
本文介绍了在C#中排序上的任意性泛型列表字母数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在任意属性泛型列表进行排序字母数字在C#?让我知道如果这个问题不明确,我会想出一个例子。

Is it possible to sort a generic list on arbitrary property alphanumerically in c#? Let me know if the question is not clear and I will come up with an example.

在此先感谢

注:我发现这个链接,做它,但不是字母数字。任何人都可以帮我吗? 的http://blog.$c$cwrench.net/2009/04/14/sorting-a-generic-list-on-arbitrary-property/

Note: I have found this link that does it but not alphanumerically. Can anyone help me out? http://blog.codewrench.net/2009/04/14/sorting-a-generic-list-on-arbitrary-property/

推荐答案

下面是一个快速字母排序(可用于其他类型与Numerics的太)。

here is a fast alphanumeric sort (can be used for other sorts with numerics too).

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());

和这里是有关该问题的一个很好的文章:

and here is a nice article about the problem:

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

Sorting for Humans : Natural Sort Order http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html

这篇关于在C#中排序上的任意性泛型列表字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆