SortedList In C#

A C# SortedList class represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index. The SortedListvclass is included in the System.Collection namespace. 
 
A SortedList object internally manages two arrays to store the elements of the list; that is, one array for the keys and another array for values. Each element is a key/value pair that can be accessed as a DictionaryEntry object. A key cannot be null but a value can be.
 
SortedList has 6 different types of constructor like SortedList(), SortedList(IComparer), etc.
 
In the SortedList index, the sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly.
 
When an element is removed, the indexing also adjusts accordingly. Therefore, the index of a specific key/value pair might change as elements are added or removed from the SortedList object.
 
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
 
The foreach statement of the C# language returns an object of the type of elements in the collection. Since each element of the SortedList object is a key/value pair, the element type is not the type of the key or the type of the value. Rather, the element type is DictionaryEntry. 
 

C# SortedList Properties

 
Keys
Gets the keys contained in the SortedList object.
Values
Gets the keys contained in the theSortedList object.
Item[Object]
Gets or sets the value associated with a specific key in a SortedList object.(object=key)
Capacity
Gets or sets the capacity of a SortedList object.
Count
Gets the number of elements contained in a SortedList object.
IsFixedSize
Gets bool value the SortedList is fixed size.
IsReadOnly
Gets a value indicating whether a SortedList object is read-only.
 

C# SortedList Methods

 
Add(Object, Object)
Adds an element with the specified key and value to a SortedList object.
Clone()
Creates a shallow copy of a SortedList object.
Contains(Object)
Return bool value, Determines whether the SortedList contains a specific key.
ContainsKey(Object)
Return bool value, Determines whether the SortedList contains a specific key.
ContainsValue(Object)
Return bool value, Determines whether the SortedList contains a specific value.
IndexOfKey(Object)
Returns the zero-based index of the specified key in a SortedList object.
IndexOfValue(Object)
Returns the zero-based index of the first occurrence of the specified value in a SortedListobject.
 
Example 1
 
The following code snippet creates a SortedList. where the key type is int and value type is a string.
  1. SortedList NumberSortedList = new SortedList();  
  2. //The following code snippet adds items(unordered) to the SortedList.   
  3. NumberSortedList.Add(4, "Four");  
  4. NumberSortedList.Add(1, "One");  
  5. NumberSortedList.Add(3, "Three");  
  6. NumberSortedList.Add(7, "Seven");  
  7. NumberSortedList.Add(5, "Five");  
  8. NumberSortedList.Add(6, "Six");  
  9. NumberSortedList.Add(2, "Two");  
  10. Console.WriteLine("--Number SortedList(sorted by the keys)--");  
  11. //The SortedList is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.  
  12. foreach (DictionaryEntry item in NumberSortedList)  
  13. {  
  14.    Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");  
  15. }  
Output
 
SortedList - C#
 
Example 2 
 
The following code snippet creates a SortedList where the value type is List<string>. 
  1. SortedList ContryNameSortedList = new SortedList();  
  2.   
  3. //The following code snippet creates a list where collection of contry name alphabet wise  
  4. List<string> ContryNameStartWith_A = new List<string> { "Afghanistan""Albania""Algeria""Andorra" };  
  5. List<string> ContryNameStartWith_B = new List<string> { "Bahamas""Bahrain""Bangladesh""Barbados" };  
  6. List<string> ContryNameStartWith_C = new List<string> { "Cambodia""Cameroon""Canada""Cape Verde" };  
  7. List<string> ContryNameStartWith_D = new List<string> { "Denmark""Djibouti""Dominica""Dominican Republic" };  
  8. List<string> ContryNameStartWith_E = new List<string> { "Egypt""Ecuador""Equatorial Guinea""East Timor" };   
  9.   
  10. // The following code snippet adds items(unordered) to the SortedList.   
  11. ContryNameSortedList.Add('A', ContryNameStartWith_A);  
  12. ContryNameSortedList.Add('D', ContryNameStartWith_D);  
  13. ContryNameSortedList.Add('C', ContryNameStartWith_C);  
  14. ContryNameSortedList.Add('B', ContryNameStartWith_B);  
  15. ContryNameSortedList.Add('E', ContryNameStartWith_E);  
  16. Console.WriteLine("--Alphabet Wise Country Name--");  
  17. //The Hashtable is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.  
  18. foreach (DictionaryEntry item in ContryNameSortedList)  
  19. {  
  20.    List<string> countrysName = ContryNameSortedList[item.Key] as List<string>;  
  21.    Console.WriteLine($"Alphabet: {item.Key}, --> Contry Name: {string.Join("", countrysName)}");  
  22. }  
Output
 
SortedList - C#
 

SortedList Clone() Example

 
The following code snippet creates a cloneSortedList clone of NumberSortedList SortedList. 
  1. SortedList cloneSortedList = NumberSortedList.Clone() as SortedList;  
  2. cloneSortedList = NumberSortedList.Clone() as SortedList;  
  3. Console.WriteLine("--Example of Clone SortedList--");  
  4. Console.WriteLine("");  
  5. Console.WriteLine("--Number SortedList--");  
  6. Console.WriteLine("");  
  7. //The SortedList is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.  
  8. foreach (DictionaryEntry item in NumberSortedList)  
  9. {  
  10.    Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");  
  11. }  
  12. Console.WriteLine("");  
  13. Console.WriteLine("--Clone Number SortedList--");  
  14. Console.WriteLine("");  
  15. //The SortedList is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.  
  16. foreach (DictionaryEntry item in cloneSortedList)  
  17. {  
  18.    Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");  
  19. }  
Output
 
SortedList - C#
 
Example of Contains(), ContainsKey() and ContainsValue()
 
The following code snippet show how to use Contains(), ContainsKey() and ContainsValue() with SortedList. 
  1. Console.WriteLine("--Example of Contains()--");  
  2. Console.WriteLine("");  
  3. Console.WriteLine($"Is the key '1' present in NumberSortedList?: {NumberSortedList.Contains(1)}");  
  4. Console.WriteLine("");  
  5. Console.WriteLine("--Example of ContainsKey()--");  
  6. Console.WriteLine("");  
  7. Console.WriteLine($"Is the key '1' present in NumberSortedList?: {NumberSortedList.ContainsKey(1)}");  
  8. Console.WriteLine($"Is the key '10' present in NumberSortedList?: {NumberSortedList.ContainsKey(10)}");  
  9. Console.WriteLine("");  
  10. Console.WriteLine("--Example of ContainsValue()--");  
  11. Console.WriteLine("");  
  12. Console.WriteLine($"Is the value 'Ten' present in NumberSortedList?: {NumberSortedList.ContainsValue("Ten")}");  
  13. Console.WriteLine($"Is the value 'One' present in NumberSortedList?: {NumberSortedList.ContainsValue("One")}");  
Output
 
SortedList - C#
 
Example of Capacity, IsFixedSize, IsReadOnly Property And IndexOfKey(), IndexOfValue() Method
 
The following code snippet show how to use Capacity, IsFixedSize, IsReadOnly Property And IndexOfKey(), IndexOfValue() with SortedList.
  1. Console.WriteLine("--Example of Capacity Property --");  
  2. Console.WriteLine("");  
  3. Console.WriteLine($"Capacity of a NumberSortedList : {NumberSortedList.Capacity}");  
  4. Console.WriteLine("");  
  5. Console.WriteLine("--Example of IsFixedSize Property --");  
  6. Console.WriteLine("");  
  7. Console.WriteLine($"NumberSortedList has a fixed size : {NumberSortedList.IsFixedSize}");  
  8. Console.WriteLine("");  
  9. Console.WriteLine("--Example of IsReadOnly Property --");  
  10. Console.WriteLine("");  
  11.   
  12. Console.WriteLine($"NumberSortedList is read-only. : {NumberSortedList.IsReadOnly}");  
  13. Console.WriteLine("");  
  14. Console.WriteLine("--Example of IndexOfKey Method --");  
  15. Console.WriteLine("");  
  16. Console.WriteLine($"'IndexOfKey(5)' Returns the index of the '5'(Key) : {NumberSortedList.IndexOfKey(5)}");  
  17. Console.WriteLine("");  
  18. Console.WriteLine("--Example of IndexOfValue Method --");  
  19. Console.WriteLine("");  
  20. Console.WriteLine($"IndexOfValue(\"Six\") Returns the index of the \"Six\"(Value) : {NumberSortedList.IndexOfValue("Six")}");  
Output
 
SortedList - C#
 
Example of Synchronized Packing
 
The following code snippet shows how to use Synchronized with SortedList.
  1. SortedList synchronizedNumberSortedList = SortedList.Synchronized(NumberSortedList);  
  2. Console.WriteLine("--Example of IsSynchronized Property --");  
  3. Console.WriteLine("");  
  4. // print the status of both SortedList   
  5. Console.WriteLine("NumberSortedList SortedList is {0}.", NumberSortedList.IsSynchronized ? "synchronized" :"not synchronized");  
  6.   
  7. Console.WriteLine("synchronizedNumberSortedList SortedList is {0}." ,synchronizedNumberSortedList.IsSynchronized ? "synchronized" : "not synchronized");   
Output
 
SortedList - C#
 

SortedList Clear() Method & Count Property Example

 
The following code snippet shows how to use Clear() Method & Count Property with SortedList. 
  1. Console.WriteLine("--Example of Clear() Method & Count Property --");  
  2. Console.WriteLine("");  
  3.   
  4. Console.WriteLine($"Before 'NumberSortedList.Clear()' Number of items in NumberSortedList= {NumberSortedList.Count}");  
  5. NumberSortedList.Clear();  
  6. Console.WriteLine($"After 'NumberSortedList.Clear()' Number of items in NumberSortedList= {NumberSortedList.Count}");  
Output
 
SortedList - C#


Recommended Free Ebook
Similar Articles