Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Cách sắp xếp ArrayList trong C#

Để sắp xếp ArrayList trong C#, dùng phương thức Sort().

ArrayList mẫu:

ArrayList arr = new ArrayList();
arr.Add(32); arr.Add(12); arr.Add(55); arr.Add(8); arr.Add(13);

Sắp xếp:

arr.Sort();

Ví dụ

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("Danh sách: ");
            foreach (int i in arr) Console.Write(i + " ");
            Console.WriteLine();
            Console.Write("Danh sách sắp xếp: ");
            arr.Sort();
            foreach (int i in arr) Console.Write(i + " ");
            Console.ReadKey();
        }
    }
}

Kết quả

Danh sách: 32 12 55 8 13
Danh sách sắp xếp: 8 12 13 32 55