SortedList क्लास की-वैल्यू पेयर्स का संग्रह है जो कुंजियों के आधार पर स्वचालित रूप से सॉर्टेड रहता है। यह IDictionary इम्प्लीमेंट करता है और कुंजियों द्वारा तेज़ लुकअप (O(log n)) प्रदान करता है।
उदाहरण 1: स्ट्रिंग कुंजियों और वैल्यूज़ के साथ SortedList
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 entry in sortedList) {
Console.WriteLine("Key = " + entry.Key + ", Value = " + entry.Value);
}
Console.WriteLine("\nकी-वैल्यू पेयर्स की संख्या = " + sortedList.Count);
// एन्यूमरेटर से इटरेशन
Console.WriteLine("\nएन्यूमरेटर से इटरेशन...");
IDictionaryEnumerator enumerator = sortedList.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine("Key = " + enumerator.Key + ", Value = " + enumerator.Value);
}
}
}
आउटपुट
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
की-वैल्यू पेयर्स की संख्या = 10
एन्यूमरेटर से इटरेशन...
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: मिश्रित प्रकार और IndexOfValue
using System;
using System.Collections;
public class Demo {
public static void Main(string[] args) {
SortedList list1 = new SortedList();
list1.Add("One", 1);
list1.Add("Two", 2);
list1.Add("Three", 3);
list1.Add("Four", 4);
list1.Add("Five", 5);
list1.Add("Six", 6);
list1.Add("Seven", 7);
list1.Add("Eight", 8);
list1.Add("Nine", 9);
list1.Add("Ten", 10);
Console.WriteLine("SortedList1 तत्व...");
foreach (DictionaryEntry entry in list1) {
Console.WriteLine(entry.Key + " " + entry.Value);
}
// वैल्यू द्वारा इंडेक्स ढूंढें
Console.WriteLine("वैल्यू 7 का इंडेक्स = " + list1.IndexOfValue(7));
SortedList list2 = new SortedList();
list2.Add("A", "Accessories");
list2.Add("B", "Books");
list2.Add("C", "Smart Wearable Tech");
list2.Add("D", "Home Appliances");
Console.WriteLine("\nSortedList2 तत्व...");
foreach (DictionaryEntry entry in list2) {
Console.WriteLine(entry.Key + " " + entry.Value);
}
Console.WriteLine("वैल्यू 'Books' का इंडेक्स = " + list2.IndexOfValue("Books"));
}
}
आउटपुट
SortedList1 तत्व...
Eight 8
Five 5
Four 4
Nine 9
One 1
Seven 7
Six 6
Ten 10
Three 3
Two 2
वैल्यू 7 का इंडेक्स = 5
SortedList2 तत्व...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances
वैल्यू 'Books' का इंडेक्स = 1
मुख्य विशेषताएं
- स्वतः सॉर्टेड: कुंजियों के आधार पर हमेशा सॉर्टेड क्रम में रहता है।
- कुंजी अद्वितीयता: डुप्लिकेट कुंजियों की अनुमति नहीं।
- इंडेक्स एक्सेस: सॉर्टेड स्थिति द्वारा इंडेक्स एक्सेस समर्थित (
GetKey(index),GetByIndex(index))। - धीमा इंसर्शन: O(n) क्योंकि सॉर्टिंग बनाए रखनी पड़ती है।
- तेज़ लुकअप: कुंजी द्वारा O(log n)।
SortedList बनाम SortedDictionary
SortedList: कम मेमोरी, तेज़ लुकअप, धीमा इंसर्शन/डिलीशन। छोटे डेटा सेट के लिए बेहतर।SortedDictionary: तेज़ इंसर्शन/डिलीशन (O(log n)), अधिक मेमोरी। बड़े डेटा सेट के लिए बेहतर।