OrderBy和OrderByDescending排序

昨天有练习对数字阵列进行排序,《C#阵列Array排序https://www.cnblogs.com/insus/p/10825174.html

其实一切都弄得很复杂,array已经有2个方法OrderBy和OrderByDescending:

参考下面代码演示:

 

 int[] ints = { 10, 45, 15, 39, 21, 26 };
           
            foreach (var i in ints.OrderBy(g => g))
            {
                System.Console.Write(i + " ");
            }


            Console.WriteLine();
            var str = new string('-', 10);
            Console.Write(str);
            Console.WriteLine();

            
            foreach (var i in ints.OrderByDescending(g => g))
            {
                System.Console.Write(i + " ");
            }
            Console.WriteLine();
Source Code

 

按Ctrl + F5运行控制台程序:

 

为了源码与存档,写成一个类:

 

 

 

posted @ 2019-05-08 10:06  Insus.NET  阅读(18074)  评论(3编辑  收藏  举报