如何在列表中进行字母数字排序 [英] How to do Alphanumeric Sorting in list

查看:131
本文介绍了如何在列表中进行字母数字排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var strTest = new List<string> { "B1", "B2","b3","0", "B3", "B10","B20","B90","GB2","B9","B21","GB1","GB11" };



我需要将上面的列表排序为

0,b3,B1,B2,B3,B9,B10, B20,B21,B90,GB1,GB2,GB11



任何人都可以帮我这个。



先谢谢,


I need to sort the above list as
"0, b3,B1, B2, B3, B9, B10, B20, B21, B90, GB1, GB2,GB11"

Can anyone help me with this one.

Thanks in Advance,

推荐答案

看看这些CodeProject文章:

自然分类比较器 [ ^ ]

使用IComparer为人类排序字符串 [ ^ ]
Take a look at these CodeProject articles:
Natural Sort Comparer[^]
Sorting Strings for Humans with IComparer[^]


您可以编写自己的比较器(参见 MSDN 列表< t> .Sort方法(比较< t>) [ ^ ]。在其实现中,您必须首先将字符串拆分为字母和数字部分(处理极端情况,如0),然后首先根据字母,然后是数字部分。
You might write you own comparer (see MSDN: "List<t>.Sort Method (Comparison<t>)"[^]. In its implementation you have to first split the strings into their alphabetic and numeric parts (handling the corner cases, like "0") and then evaluate the respective order based first on the alphabetic and then on the numeric parts.


谢谢大家...

它工作得很好..

Thanks Everyone...
Its working nicely..
static void Main()
{
    string[] highways = new string[] { "B1", "B2", "b3", "0", "B3", "B10", "B20", "B90", "B9", "B21", "GB1", "GB11" };
    Array.Sort(highways, new AlphanumComparatorFast());

    foreach (string item in highways)
    {
        Console.WriteLine(item);
    }

    Console.ReadLine();
}







public class AlphanumComparatorFast : System.Collections.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;

            while (marker1 < len1 && marker2 < len2)
            {
                char ch1 = s1[marker1];
                char ch2 = s2[marker2];

                char[] space1 = new char[len1];
                int loc1 = 0;
                char[] space2 = new char[len2];
                int loc2 = 0;

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

                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;
        }
    }


这篇关于如何在列表中进行字母数字排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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