Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a SortedList in C#?
A SortedList in C# is a collection that stores key-value pairs in sorted order based on the keys. It combines the functionality of arrays and hashtables, automatically maintaining elements in ascending order of keys. Unlike regular lists, SortedList provides efficient searching and maintains order without manual sorting.
Syntax
Following is the syntax for creating a SortedList −
SortedList sortedList = new SortedList(); sortedList.Add(key, value);
You can also specify initial capacity −
SortedList sortedList = new SortedList(capacity);
Key Features
-
Automatic Sorting: Elements are automatically sorted by key in ascending order.
-
Key-Value Pairs: Each element consists of a unique key and its associated value.
-
Index Access: Elements can be accessed by key or by zero-based index.
-
Unique Keys: Duplicate keys are not allowed; adding a duplicate key will throw an exception.
Using Basic SortedList Operations
Example
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 elements...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of SortedList key-value pairs = " + sortedList.Count);
Console.WriteLine("\nEnumerator to iterate through the SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
The output of the above code is −
SortedList elements... 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 Count of SortedList key-value pairs = 10 Enumerator to iterate through the 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
Using IndexOfValue Method
Example
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 elements...");
foreach(DictionaryEntry d in list1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Index at value 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 elements...");
foreach(DictionaryEntry d in list2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Index at value Books = " + list2.IndexOfValue("Books"));
}
}
The output of the above code is −
SortedList1 elements... Eight 8 Five 5 Four 4 Nine 9 One 1 Seven 7 Six 6 Ten 10 Three 3 Two 2 Index at value 7 = 5 SortedList2 elements... A Accessories B Books C Smart Wearable Tech D Home Appliances Index at value Books = 1
Common Methods
| Method | Description |
|---|---|
| Add(key, value) | Adds a key-value pair to the SortedList |
| Remove(key) | Removes the element with the specified key |
| ContainsKey(key) | Checks if the specified key exists |
| ContainsValue(value) | Checks if the specified value exists |
| IndexOfKey(key) | Returns the zero-based index of the specified key |
| IndexOfValue(value) | Returns the zero-based index of the specified value |
Conclusion
SortedList in C# provides an efficient way to store key-value pairs in automatically sorted order. It combines the benefits of arrays and hashtables, offering both indexed access and key-based lookups while maintaining elements sorted by keys in ascending order.
