列表进行排序包含有串字母/数字 [英] Sorting of list contained strings having alphabetic/numeric

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

问题描述

我想使其中包含字符串按字母顺序串,字母和数字和数字only.I混合有要求的排序列表从我的客户端进行排序是这样的:



排序在tablelist类型的对象包含商品编号:111,111A,222,411G,300,411Z,G411,AG500,A111,AZ600,ABQ,ZZZ,AAN等



必需结果:首秀号码(如111,然后222然后300等等)接下来将用字母数字(如111A然后411G然后411Z等...)然后用数字(如A111则G411则AG500然后AZ600等信...)下一个字母只(如AAN然后ABQ然后ZZZ等....)



因此字符串可以anything.But我想要得到那种要求结果。所以,请帮我有关吧。


解决方案

  unsortedStringList.Sort(新AlphanumComparatorFastString()) ; 



AlphanumComparator:



 公共类AlphanumComparatorFastString:的IComparer<串GT; 
{
公众诠释比较(字符串S1,S2线)
{
如果(S1 == NULL)
返回0;

如果(S2 == NULL)
返回0;

INT LEN1 = s1.Length;
INT LEN2 = s2.Length;
INT MARKER1 = 0;
INT MARKER2 ​​= 0;

//通过两根琴弦与两个标记的步行路程。
,而(MARKER1<&LEN1功放;&安培; MARKER2< LEN2)
{
焦炭CH1 = S1 [MARKER1]
焦炭CH2 = S2 [MARKER2]

//一些缓冲,我们可以建立在每个块的字符。
的char [] = space1新的char [LEN1]
INT LOC1 = 0;
的char [] = space2新的char [LEN2]
INT LOC2 = 0;

//通过以下所有字符都开始相应的标记两个字符串的数字或
//人物行走。
//收集字符数组。

{
space1 [LOC1 ++] = CH1;
MARKER1 ++;

如果(MARKER1< LEN1)
{
CH1 = S1 [MARKER1]
}
,否则
{
中断;
}
},而(char.IsDigit(CH1)== char.IsDigit(space1 [0]));


{
space2 [LOC2 ++] = CH2;
MARKER2 ​​++;

如果(MARKER2< LEN2)
{
2 = S2 [MARKER2]
}
,否则
{
中断;
}
}当(char.IsDigit(CH 2)== char.IsDigit(space2 [0]));

//如果我们已经收集的数字,数字进行比较。
//否则,如果我们有一个字符串,按字母顺序进行比较。
串STR1 =新的字符串(space1);
串str2的=新的字符串(space2);

INT结果;

如果(char.IsDigit(space1 [0])及&放大器; char.IsDigit(space2 [0]))
{
INT thisNumericChunk = int.Parse(STR1 );
INT thatNumericChunk = int.Parse(STR2);
结果= thisNumericChunk.CompareTo(thatNumericChunk);
}
,否则
{
结果= str1.CompareTo(STR2);
}

如果(结果!= 0)
{
返回结果;
}
}
返回LEN1 - LEN2;
}
}



作为的 http://www.dotnetperls.com/alphanumeric-sorting


I want to make sort list of strings which contains strings of alphabetically, mixture of alphabetic and numeric and numeric only.I have requirement from my client to sort like this:

Sort in tablelist type object contains Item Code: 111,111A,222,411G,300,411Z,G411,AG500,A111,AZ600,ABQ,ZZZ,AAN etc

Required result: First show numbers (like 111 then 222 then 300 etc...) Next will be numbers with Letters (like 111A then 411G then 411Z etc...) Next Letters with numbers (like A111 then G411 then AG500 then AZ600 etc. ...) next letters only (like AAN then ABQ then ZZZ etc....)

So the string can be anything.But I want to get sort as required result. So please help me regarding it.

解决方案

unsortedStringList.Sort(new AlphanumComparatorFastString());

AlphanumComparator:

    public class AlphanumComparatorFastString : IComparer<String>
    {
        public int Compare(string s1, string s2)
        {
            if (s1 == null)
                return 0;

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

as found on http://www.dotnetperls.com/alphanumeric-sorting

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

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