เพื่อจัดเรียง ArrayList ใน C# ใช้เมธอด Sort()
นี่คือ ArrayList:
ArrayList arr = new ArrayList(); arr.Add(32); arr.Add(12); arr.Add(55); arr.Add(8); arr.Add(13);
ใช้เมธอด Sort() เพื่อจัดเรียง:
arr.Sort();
ตัวอย่าง
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
ArrayList arr = new ArrayList();
arr.Add(32); arr.Add(12); arr.Add(55); arr.Add(8); arr.Add(13);
Console.Write("List: ");
foreach (int i in arr) Console.Write(i + " ");
Console.WriteLine();
Console.Write("Sorted List: ");
arr.Sort();
foreach (int i in arr) Console.Write(i + " ");
Console.WriteLine();
Console.ReadKey();
}
}
}
ผลลัพธ์
List: 32 12 55 8 13 Sorted List: 8 12 13 32 55