簡體   English   中英

列表的排序包含具有字母/數字的字符串

[英]Sorting of list contained strings having alphabetic/numeric

我想創建一個字符串排序列表,其中包含字母字符串,僅字母數字和數字以及數字的混合。我要求客戶進行如下排序:

在表列表中排序類型對象包含項目代碼: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等...)

所以字符串可以是任何東西。但是我想按要求的結果進行排序。 所以請幫我。

unsortedStringList.Sort(new AlphanumComparatorFastString());

Alphanum比較器:

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

http://www.dotnetperls.com/alphanumeric-sorting上所示

嘗試這個:

var text = "111,111A,222,411G,300,411Z,G411,AG500,A111,AZ600,ABQ,ZZZ,AAN";
var list = text.Split(',').ToList();
var result = list.OrderBy(i => i, new StringCompare());
foreach (var item in result)
{
    Console.WriteLine(item);
}

StringCompare類:

class StringCompare : IComparer<string>
{
    string[] exps = new[] { @"^\d+$", @"^\d+[a-zA-Z]+$", @"^[a-zA-Z]\d+$", @"^[a-zA-Z]+\d+$" };
    public int Compare(string x, string y)
    {
        for (int i = 0; i < exps.Length; i++)
        {
            var isNumberx = Regex.IsMatch(x, exps[i]);
            var isNumbery = Regex.IsMatch(y, exps[i]);

            if (isNumberx && isNumbery)
                return string.Compare(x, y);
            else if (isNumberx)
                return -1;
            else if (isNumbery)
                return 1;
            //return string.Compare(x, y);
        }
        return string.Compare(x, y);
    }
}

你會得到:

111
222
300
111A
411G
411Z
A111
G411
AG500
AZ600
AAN
ABQ
ZZZ

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM