SortedList lưu trữ các cặp key-value được sắp xếp theo key. Để duyệt qua các phần tử, bạn có thể sử dụng phương thức GetEnumerator() trả về IDictionaryEnumerator, hoặc dùng foreach trực tiếp (khuyến nghị).
Ví dụ 1: Duyệt bằng IDictionaryEnumerator
using System;
using System.Collections;
public class Demo
{
public static void Main(string[] args)
{
SortedList sortedList = new SortedList();
sortedList.Add("A", "1");
sortedList.Add("B", "2");
sortedList.Add("C", "3");
sortedList.Add("D", "4");
sortedList.Add("E", "5");
sortedList.Add("F", "6");
sortedList.Add("G", "7");
sortedList.Add("H", "8");
sortedList.Add("I", "9");
sortedList.Add("J", "10");
Console.WriteLine("Các phần tử của SortedList...");
foreach (DictionaryEntry d in sortedList)
{
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Số cặp key-value của SortedList = " + sortedList.Count);
Console.WriteLine("\nEnumerator duyệt qua SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
Kết quả
Các phần tử của SortedList... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10 Số cặp key-value của SortedList = 10 Enumerator duyệt qua SortedList... Key = A, Value = 1 Key = B, Value = 2 Key = C, Value = 3 Key = D, Value = 4 Key = E, Value = 5 Key = F, Value = 6 Key = G, Value = 7 Key = H, Value = 8 Key = I, Value = 9 Key = J, Value = 10
Ví dụ 2: Key là string không theo thứ tự alphabet
using System;
using System.Collections;
public class Demo
{
public static void Main(string[] args)
{
SortedList sortedList = new SortedList();
sortedList.Add("One", "1");
sortedList.Add("Two", "2");
sortedList.Add("Three", "3");
sortedList.Add("Four", "4");
sortedList.Add("Five", "5");
sortedList.Add("Six", "6");
sortedList.Add("Seven", "7");
sortedList.Add("Eight", "8");
sortedList.Add("Nine", "9");
sortedList.Add("Ten", "10");
Console.WriteLine("Enumerator duyệt qua SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
Kết quả (sắp xếp theo key alphabet)
Enumerator duyệt qua SortedList... Key = Eight, Value = 8 Key = Five, Value = 5 Key = Four, Value = 4 Key = Nine, Value = 9 Key = One, Value = 1 Key = Seven, Value = 7 Key = Six, Value = 6 Key = Ten, Value = 10 Key = Three, Value = 3 Key = Two, Value = 2
Cách hiện đại: foreach (Khuyến nghị)
SortedList sortedList = new SortedList();
sortedList.Add("A", "1");
// ...
// Cách 1: DictionaryEntry (non-generic)
foreach (DictionaryEntry entry in sortedList)
{
Console.WriteLine($"Key = {entry.Key}, Value = {entry.Value}");
}
// Cách 2: Generic SortedList<TKey, TValue>
var genericSortedList = new SortedList<string, int>();
genericSortedList.Add("A", 1);
// ...
foreach (var kvp in genericSortedList)
{
Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
}
// Cách 3: Deconstruction (C# 7+)
foreach (var (key, value) in genericSortedList)
{
Console.WriteLine($"Key = {key}, Value = {value}");
}
Đặc điểm của SortedList
| Đặc điểm | Mô tả |
|---|---|
| Sắp xếp | Key tăng dần (IComparable) |
| Cấu trúc | 2 mảng song song (keys[], values[]) |
| Truy xuất index | O(1) - GetKey(i), GetByIndex(i) |
| Insert/Remove | O(n) - dịch chuyển mảng |
| Bộ nhớ | Tiết kiệm hơn SortedDictionary |
Truy xuất theo Index (Đặc quyền của SortedList)
SortedList list = new SortedList();
list.Add("C", 3);
list.Add("A", 1);
list.Add("B", 2);
// Sắp xếp: A, B, C
Console.WriteLine(list.GetKey(0)); // "A"
Console.WriteLine(list.GetByIndex(0)); // 1
Console.WriteLine(list.GetKey(1)); // "B"
Console.WriteLine(list.GetByIndex(1)); // 2
// Generic version
SortedList<string, int> glist = new SortedList<string, int>();
glist.Add("C", 3);
glist.Add("A", 1);
glist.Add("B", 2);
Console.WriteLine(glist.Keys[0]); // "A"
Console.WriteLine(glist.Values[0]); // 1
Lưu ý
IDictionaryEnumeratorlà interface cũ, ít dùng trong code hiện đại- Non-generic
SortedListtrongSystem.Collections - Generic
SortedList<TKey, TValue>trongSystem.Collections.Generic- khuyến nghị dùng - Key không được null, Value có thể null
- Không cho phép key trùng lặp