按数值,字母值和符号对列表进行排序

时间:2013-08-13 16:26:05

标签: c# linq sorting compare icomparer

嗨,我一直绞尽脑汁想办法做到这一点,但我不能。假设我有以下列表:

1
2
'
@
r
r2
r1

我希望按照以下顺序排序

1
2
r
r1
r2
'
@

我有以下代码(无法弄清楚最后在哪里进行符号排序)

void Main()
{


List<string> list = new List<string>();
list.Add("1");
list.Add("2");
list.Add("'");
list.Add("@");
list.Add("r");
list.Add("r2");
list.Add("r1");
list.Sort(new AlphanumComparatorFastString());

}

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(sp`enter code here`ace1[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;
    }
    }



Thanks for the help

3 个答案:

答案 0 :(得分:4)

var ordered = list.OrderByDescending(x => x.All(char.IsDigit))
 .ThenByDescending(x=> x.Any(char.IsLetter))
 .ThenBy(x=>x)
 .ToList();

结果:

1 
2 
r
r1 
r2 
' 
@ 

答案 1 :(得分:1)

我拆分你的逻辑

var list = new List<string>{"1", "2", "'", "@", "r", "r2", "r1"};

//Process the list into segments/classes for ordering
var ordered = list
    .Select(d => new { OrderBy = GetOrderByClass(d), Value = d })
    .OrderBy(d => d.OrderBy)
    .ThenBy(d => d.Value)
    .Select(d => d.Value)
    .ToList();


//Get's a segment/class against an input type
public int GetOrderByClass(string value)
{   
    //Numbers
    if(Regex.IsMatch(value, "$\\d+"))
        return 0; 

    //Alpha
    if(Regex.IsMatch(value, "[a-zA-Z0-9]+"))
        return 1;

    //Everything else
    return 2;
}

答案 2 :(得分:0)

循环所有s1和s2的角色,直到你得到2个不同的角色 - 如果所有字符都相同,则返回0.

比较2个字符 - 如果它们是不同类型(数字,字符,字母),则比较类型
- 如果它们是相同类型,则比较字符