![]() |
VOOZH | about |
In C#, SortedList is a collection of key-value pairs sorted according to keys. By default, this collection sorts in ascending order. It is of both generic and non-generic type of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic SortedList is defined under System.Collections namespace, here we will discuss the non-generic type SortedList.
Example:
Key: 1, Value: One Key: 2, Value: Two Key: 3, Value: Three
We can use the SortedList Class which provides different ways to create a Sorted list. Here, we use the SortedList Class constructor to create a sorted list. Which is used to create an instance of the SortedList class that is empty by default and sorted according to the IComparable interface implemented by each key added to the SortedList object.
using System.Collections;
SortedList list_name = new SortedList();
For adding elements list, The SortedList class provides the following methods:
Syntax:
// Add element using Add method
list.Add(key, value);// Adding elements using the
// Collection initializers
SortedList<TKey, TValue> mySortedList = new SortedList<int, string>(){
{ key, value },
{ key, value },
};
Example: Creating sortedList using different ways.
1.02 and This 1.04 and SortedList 1.07 and Is b.01 and 405 b.09 and 234 b.11 and 395 b.67 and 100
There are three different ways to access the elements of the SortedList as it is stored in the Key-Value pair we can get the Key and value separately.
Using for Loop: We can use Loop to iterate through the list and access key-value pairs we can use List.GetKey() method to access the key and similarly from the key we can access the value by using List.GetByIndex(key).
Syntax:
for (int i = 0; i < sList.Count; i++)
{
Console.WriteLine("{0} and {1}",
sList.GetKey(i), // Accessing keys
sList.GetByIndex(i)); // Accessing values
}
Using ForEach Loop: We can use a foreach loop to access the key-value pairs of the SortedList. In the foreach loop, we can use a dictionary. It is mostly used to access the list stored in key-value pairs.
Syntax:
foreach(DictionaryEntry pair in my_slist1)
{
Console.WriteLine("{0} and {1}",
pair.Key, pair.Value);
}
Using Indexers: We can access the individual value of the SortedList by using the index. We need to pass the key or index as a parameter to find the respective value. It is similar to how we access the array.
Syntax:
Console.WriteLine("Value is:{0}", my_slist1[1.04]);
string x = (string)my_slist[1.02];
Console.WriteLine(x);
Note: If the specified key is not available, then the a runtime exception will be thrown (KeyNotFoundException).
Example: Demonstration of accessing SortedList in different ways.
Access using for loop 1: Geek1 2: Geek2 3: Geek3 Access using foreach loop 1: Geek1 2: Geek2 3: Geek3 Access using indexer Key 2: Geek2
Example:
1 and one 2 and two 3 and three 2 and two 3 and three 2 and two Total pairs present in sorted list is: 0
We can check the element of the SortedList by using the methods available below it returns value in boolean if the the element present returns true either false.
Example:
Key is found...!! Key is found...!! Value is found...!!