SortedList ให้เมธอด GetEnumerator() คืนค่า IDictionaryEnumerator สำหรับวนลูปคู่คีย์-ค่าที่เรียงลำดับตามคีย์แล้ว
ตัวอย่างที่ 1: SortedList 10 รายการ
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("SortedList องค์ประกอบ...");
foreach (DictionaryEntry d in sortedList)
{
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("จำนวนคู่คีย์-ค่าใน SortedList = " + sortedList.Count); // 10
Console.WriteLine("\nEnumerator วนลูปผ่าน SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
ผลลัพธ์ (เรียงตามคีย์ A-J)
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 จำนวนคู่คีย์-ค่าใน SortedList = 10 Enumerator วนลูปผ่าน 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
ตัวอย่างที่ 2: SortedList คีย์เป็นคำอ่าน (เรียงตามอักษร)
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 วนลูปผ่าน SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
}
ผลลัพธ์ (เรียงตามอักษร: Eight, Five, Four...)
Enumerator วนลูปผ่าน 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
วิธีการวนลูปแบบสมัยใหม่ (แนะนำ)
// 1. ใช้ foreach กับ DictionaryEntry (Non-Generic)
foreach (DictionaryEntry entry in sortedList)
{
Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
}
// 2. แปลงเป็น Generic SortedList<TKey, TValue> (แนะนำ)
var genericSortedList = new SortedList<string, string>();
genericSortedList.Add("A", "1");
// ...
foreach (var kvp in genericSortedList)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
// 3. ใช้ Keys/Values แยกกัน
foreach (var key in sortedList.Keys)
{
Console.WriteLine($"Key: {key}, Value: {sortedList[key]}");
}
// 4. LINQ
sortedList.Cast<DictionaryEntry>().ToList().ForEach(e => Console.WriteLine($"{e.Key}: {e.Value}"));
SortedList vs SortedDictionary
| คุณสมบัติ | SortedList | SortedDictionary |
|---|---|---|
| โครงสร้าง | 2 Array (Keys, Values) เรียงแล้ว | Red-Black Tree |
| Memory | Compact (น้อยกว่า) | มากกว่า (Node/Pointer) |
| Lookup | O(log n) Binary Search | O(log n) Tree |
| Insert/Delete | O(n) - ต้อง Shift Array | O(log n) |
| Get by Index | รองรับ (GetKey, GetByIndex) | ไม่รองรับโดยตรง |
| Generic | SortedList<TKey, TValue> | SortedDictionary<TKey, TValue> |
ข้อสังเกต
- Non-generic
SortedListใช้objectแนะนำใช้SortedList<TKey, TValue> - Enumerator คืนค่าตามลำดับคีย์ที่เรียงแล้วเสมอ
- เหมาะสำหรับข้อมูลที่เพิ่ม/ลบน้อย แต่อ่านมาก (Lookup Heavy)
- คีย์ต้อง Implement
IComparableหรือระบุIComparer