Đầu tiên, hãy đặt mảng không được sắp xếp.
int[] list = {98, 23, 97, 36, 77}; Sắp xếp mảng bằng phương thức Sort ().
Array.Sort(list);
Bạn có thể thử chạy đoạn mã sau để sắp xếp một mảng theo thứ tự tăng dần.
Ví dụ
using System;
namespace Demo {
public class MyApplication {
public static void Main(string[] args) {
int[] list = {98, 23, 97, 36, 77};
Console.WriteLine("Original Unsorted List");
foreach (int i in list) {
Console.Write(i + " ");
}
Array.Sort(list);
Console.WriteLine("\nSorted List");
for(int i=0; i<list.Length; i++) {
Console.Write(list[i] + " ");
}
}
}
} Đầu ra
Original Unsorted List 98 23 97 36 77 Sorted List 23 36 77 97 98