昨天有练习对数字阵列进行排序,《C#阵列Array排序》https://www.cnblogs.com/insus/p/10825174.html
其实一切都弄得很复杂,array已经有2个方法OrderBy和OrderByDescending:
参考下面代码演示:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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();
按Ctrl + F5运行控制台程序:
为了源码与存档,写成一个类: