C# 以数字结尾字符和数字混合数据的排序问题

      //using System.Linq;

        static void Main(string[] args)

        {

            string[] coms = new string[] { "CON1""CON10""CON12""CON13""CON26""CON5""CON6""CON7" };

            var sorted = coms.Select(x => new

                {

                    x,

                    num = Convert.ToInt32(x.Replace("CON"""))

                })

                .OrderBy(x => x.num)

                .Select(x => x.x);

 

            foreach (string in sorted)

                Console.WriteLine(s);

            Console.ReadLine();

        }

 

 

 

string[] str = new string[] { "CON1""CON10""CON12""CON13""CON26""CON5""CON6""CON7" };

 

            var result = from in str orderby Convert.ToInt32(n.Replace("CON""")) ascending select n;

            string[] sort = result.ToArray();

Guess you like

Origin blog.csdn.net/shan1774965666/article/details/107458954