Sorting List in C#

后端 未结 7 1611
囚心锁ツ
囚心锁ツ 2020-11-28 12:01

How to sort a List based on the item\'s integer value

The list is like

\"1\"
\"5\"
\"3\"
\"6\"
\"11\"
\"9\"
\"NUM1\"
\"NUM0\"

The r

相关标签:
7条回答
  • 2020-11-28 12:56

    This is the Fastest Algorithm - took me 2 mili to sort 50 items ~

    static void Sort()
    {
        string[] partNumbers = new string[] {"A1", "A2", "A10", "A111"};
        string[] result = partNumbers.OrderBy(x => PadNumbers(x)).ToArray();
    }
    
    
    public static string PadNumbers(string input)
    {
            const int MAX_NUMBER_LEN = 10;
    
            string newInput = "";
            string currentNumber = "";
            foreach (char a in input)
            {
                if (!char.IsNumber(a))
                {
                    if (currentNumber == "")
                    {
                        newInput += a;
                        continue;
                    }
                    newInput += "0000000000000".Substring(0, MAX_NUMBER_LEN - currentNumber.Length) + currentNumber;
                    currentNumber = "";
                }
                currentNumber += a;
            }
            if (currentNumber != "")
            {
                newInput += "0000000000000".Substring(0, MAX_NUMBER_LEN - currentNumber.Length) + currentNumber;
            }
    
            return newInput;
        }
    

    ~

    0 讨论(0)
提交回复
热议问题