- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace p99
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] a = { 2, 5, 4, 1, 3 };
- Console.Write("当前排序:");
- foreach (int i in a)
- { Console.Write(i.ToString()); }
- Console.Write("\n");
- Array.Sort(a);
- Console.Write("排序后顺序:");
- foreach (int i in a)
- { Console.Write(i.ToString()); }
- Console.Write("\n");
- Array.Reverse(a);
- Console.Write("降序后顺序:");
- foreach (int i in a)
- { Console.Write(i.ToString()); }
- Console.Write("\n");
- }
- }
- }
运行结果:
其中Array.Sort(数组名称)是将数组元素升序排序;Array.Reverse(数组名称)与Sort结合可以实现降序排序。如上面的程序。