Mảng chưa sắp xếp:
int[] list = { 98, 23, 97, 36, 77 };
Đầu tiên dùng Sort() để sắp xếp tăng dần, sau đó dùng Reverse() để đảo ngược thành giảm dần.
Array.Sort(list); Array.Reverse(list);
Ví dụ
using System;
namespace Demo {
public class MyApplication {
public static void Main(string[] args) {
int[] list = { 98, 23, 97, 36, 77 };
Console.WriteLine("Danh sách gốc chưa sắp xếp");
foreach (int i in list) {
Console.Write(i + " ");
}
// sắp xếp
Array.Sort(list);
// Giảm dần
Array.Reverse(list);
Console.WriteLine("\nDanh sách đã sắp xếp");
for (int i = 0; i < list.Length; i++) {
Console.Write(list[i] + " ");
}
}
}
}
Kết quả
Danh sách gốc chưa sắp xếp 98 23 97 36 77 Danh sách đã sắp xếp 98 97 77 36 23