C# SortedList Tutorial

Introduction


C# SortedList<> class represents a collection of key/value pairs that are sorted by the key. C# SortedList is a generic collection, defined as SortedList<TKey, TValue>, where Tkey is the type of keys in the collection and Tvalue is the type of values in the collection. In this article, I explain how to create a SortedList object and use SortedList methods and SortedList properties on the collection.
 

Create SortedList

 
C# SortedList class is defined in the System.Collections namespace. Make sure to import the namespace in your code before using it.
We can create a SortedList object as following: 
  1. SortedList<intstring> sortedlist = new SortedList<intstring>();  
Here, the key is an int type and the value is a string type. You can create whatever type you like.

Add Items to SortedList

 
SortedList.Add() method adds items to the collection. The following code snippet adds several items to the colleciton. 
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             //display the elements of the sortedlist  
  15.             Console.WriteLine("The elements in the SortedList are:");  
  16.             foreach(KeyValuePair < intstring > pair in sortedlist) {  
  17.                 Console.WriteLine("{0} => {1}", pair.Key, pair.Value);  
  18.             }  
  19.         }  
  20.     }  
  21. }  
The above code also reads the SortedList collection items using a foreach loop and read using a KeyValuePair object. The read items are displayed on the console.
 
The output looks like the following:
 
sortedlist1.jpg
 

SortedList Properties

 
Let's look at some of the common properties of SortedList.
 

SortedList.Capacity

 
The Capacity property gets or sets the number of elements that the SortedList<TKey, TValue> can contain. The default value of capacity is 4, which allows 4 elements to be added to the list, and if we add a 5th element to the collection, the capacity increases by 4 more, which means if there are 5 elements in the list then the capacity is 8.
 
In the following example, we add an element with the number 7, so is the capacity is 8; if we add 9th elements, the capacity will be 16 (double the previous one).
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             //display the elements of the sortedlist  
  15.             Console.WriteLine("The elements in the SortedList are:");  
  16.             foreach(KeyValuePair < intstring > pair in sortedlist) {  
  17.                 Console.WriteLine("{0} => {1}", pair.Key, pair.Value);  
  18.             }  
  19.             //Find the capacity  
  20.             Console.WriteLine("The capacity is:" + sortedlist.Capacity);  
  21.             sortedlist.Add(8, "januray");  
  22.             sortedlist.Add(9, "april");  
  23.             Console.WriteLine("After adding two more element the capacity is:" + sortedlist.Capacity);  
  24.         }  
  25.     }  
  26. }  
The output looks like the following:
 
sortedlist2.jpg
 

SortedList.Count

 
The Count property counts the number of elements in the SortedList or we can say that Count gets the number of key/value pairs contained in the SortedList<TKey, TValue>. For example:
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             //display the elements of the sortedlist  
  15.             Console.WriteLine("The elements in the SortedList are:");  
  16.             foreach(KeyValuePair < intstring > pair in sortedlist) {  
  17.                 Console.WriteLine("{0} => {1}", pair.Key, pair.Value);  
  18.             }  
  19.             Console.WriteLine("The total number of elements in the sortedlist are:" + sortedlist.Count);  
  20.             sortedlist.Add(8, "januray");  
  21.             sortedlist.Add(9, "april");  
  22.             Console.WriteLine("After adding two more element the number of element in sortedlist are:" + sortedlist.Count);  
  23.         }  
  24.     }  
  25. }  
The output looks like the following:
 
sortedlist3.jpg

Difference between Capacity and Count


difference.jpg

SortedList.Keys 

 
The Keys property gets a collection containing the keys in the SortedList<TKey, TValue>; for this we use IList, for example:
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             //display the elements of the sortedlist  
  15.             Console.WriteLine("The elements in the SortedList are:");  
  16.             foreach(KeyValuePair < intstring > pair in sortedlist) {  
  17.                 Console.WriteLine("{0} => {1}", pair.Key, pair.Value);  
  18.             }  
  19.             IList < int > ilistKeys = sortedlist.Keys;  
  20.             Console.WriteLine();  
  21.             Console.WriteLine("The keys are:");  
  22.             Console.Write("{");  
  23.             foreach(int i in ilistKeys) {  
  24.                 Console.Write(i + ",");  
  25.             }  
  26.             Console.WriteLine("}");  
  27.         }  
  28.     }  
  29. }  
The output looks like the following:
 
sortedlist4.jpg
 

SortedList.Values

 
The Values property gets a collection containing the values in the SortedList<TKey, TValue>. For example:
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             //display the elements of the sortedlist  
  15.             Console.WriteLine("The elements in the SortedList are:");  
  16.             foreach(KeyValuePair < intstring > pair in sortedlist) {  
  17.                 Console.WriteLine("{0} => {1}", pair.Key, pair.Value);  
  18.             }  
  19.             IList < string > ilistvalues = sortedlist.Values;  
  20.             Console.WriteLine();  
  21.             Console.WriteLine("The Values are:");  
  22.             Console.WriteLine();  
  23.             Console.Write("{");  
  24.             foreach(string i in ilistvalues) {  
  25.                 Console.Write(i + ",");  
  26.             }  
  27.             Console.WriteLine("}");  
  28.         }  
  29.     }  
  30. }  
The output looks like the following: 
 
sortedlist5.jpg
 

SortedList.Methods

 
Here is a list of some of the useful SortedList class methods.
 

SortedList.ContainsKey()

 
This method determines whether the SortedList<TKey, TValue> contains a specific key. For example:
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             //display the elements of the sortedlist  
  15.             Console.WriteLine("The elements in the SortedList are:");  
  16.             foreach(KeyValuePair < intstring > pair in sortedlist) {  
  17.                 Console.WriteLine("{0} => {1}", pair.Key, pair.Value);  
  18.             }  
  19.             //apply contain key method  
  20.             Console.WriteLine("The key 1 contain in the SortedList:" + sortedlist.ContainsKey(1));  
  21.             Console.WriteLine("The key 5 contain in the SortedList:" + sortedlist.ContainsKey(5));  
  22.             Console.WriteLine("The key 10 contain in the SortedList:" + sortedlist.ContainsKey(10));  
  23.             Console.WriteLine("The key 50 contain in the SortedList:" + sortedlist.ContainsKey(50));  
  24.         }  
  25.     }  
  26. }  
The output looks like the following:
 
 containkey.jpg
 

SortedList.ContainsValue()

 
This method determines whether the SortedList<TKey, TValue> contains a specific value. For example:
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             //display the elements of the sortedlist  
  15.             Console.WriteLine("The elements in the SortedList are:");  
  16.             foreach(KeyValuePair < intstring > pair in sortedlist) {  
  17.                 Console.WriteLine("{0} => {1}", pair.Key, pair.Value);  
  18.             }  
  19.             //apply contain value method  
  20.             Console.WriteLine("The value Sunday contain in the SortedList:" + sortedlist.ContainsValue("Sunday"));  
  21.             Console.WriteLine("The value Friday contain in the SortedList:" + sortedlist.ContainsValue("Friday"));  
  22.             Console.WriteLine("The value May contain in the SortedList:" + sortedlist.ContainsValue("May"));  
  23.             Console.WriteLine("The value Wednesday contain in the SortedList:" + sortedlist.ContainsValue("Wednesday"));  
  24.         }  
  25.     }  
  26. }  
The output looks like the following:
 
containvalue.jpg

SortedList.Clear()

 
This method removes all elements from the SortedList<TKey, TValue>. For example:
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             Console.WriteLine("The number of elements in the sortedlist are:" + sortedlist.Count);  
  15.             //apply clear method  
  16.             sortedlist.Clear();  
  17.             Console.WriteLine("After clear method the elements in the sortedlist are:" + sortedlist.Count);  
  18.         }  
  19.     }  
  20. }  
The output looks like the following: 
 
clear.jpg
 

SortedList.IndexOfKey()

 
This method searches the specified key and returns the index value of that key.
 

SortedList.IndexOfValue()

 
This method searches the specified value and returns the index value of that value.
 
The following example shows the use of the both IndexOfKey and IndexOfValue methods
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             //IndexOfKey method  
  15.             Console.WriteLine("***************INDEXOFKEY***************");  
  16.             Console.WriteLine();  
  17.             Console.WriteLine("The index value of the key 4 is:" + sortedlist.IndexOfKey(4));  
  18.             Console.WriteLine("The index value of the key 1 is:" + sortedlist.IndexOfKey(1));  
  19.             Console.WriteLine("The index value of the key 7 is:" + sortedlist.IndexOfKey(7));  
  20.             Console.WriteLine("The index value of the key 2 is:" + sortedlist.IndexOfKey(2));  
  21.             //IndexofValue method  
  22.             Console.WriteLine();  
  23.             Console.WriteLine("***************INDEXOFVALUE***************");  
  24.             Console.WriteLine();  
  25.             Console.WriteLine("The index value of the value Sunday is:" + sortedlist.IndexOfValue("Sunday"));  
  26.             Console.WriteLine("The index value of the value Wednesday is:" + sortedlist.IndexOfValue("Wednesday"));  
  27.             Console.WriteLine("The index value of the value Monday is:" + sortedlist.IndexOfValue("Monday"));  
  28.             Console.WriteLine("The index value of the value Friday is:" + sortedlist.IndexOfValue("Friday"));  
  29.         }  
  30.     }  
  31. }  
The output looks like the following: 
 
indexofkey.jpg
 

SortedList.Remove()

 
This method removes the element with the specified key from the SortedList<TKey, TValue>. For example:
  1. namespace SortedList {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             //creation of sortedlist  
  5.             SortedList < intstring > sortedlist = new SortedList < intstring > ();  
  6.             //add the elements in sortedlist  
  7.             sortedlist.Add(1, "Sunday");  
  8.             sortedlist.Add(2, "Monday");  
  9.             sortedlist.Add(3, "Tuesday");  
  10.             sortedlist.Add(4, "Wednesday");  
  11.             sortedlist.Add(5, "Thusday");  
  12.             sortedlist.Add(6, "Friday");  
  13.             sortedlist.Add(7, "Saturday");  
  14.             //display the elements of the sortedlist  
  15.             Console.WriteLine("The elements in the SortedList are:");  
  16.             foreach(KeyValuePair < intstring > pair in sortedlist) {  
  17.                 Console.WriteLine("{0} => {1}", pair.Key, pair.Value);  
  18.             }  
  19.             //remove the elements from the sortedlist  
  20.             sortedlist.Remove(3);  
  21.             sortedlist.Remove(7);  
  22.             sortedlist.Remove(1);  
  23.             Console.WriteLine("After remove some elements the sortedlist is as:");  
  24.             foreach(KeyValuePair < intstring > pair1 in sortedlist) {  
  25.                 Console.WriteLine("{0} => {1}", pair1.Key, pair1.Value);  
  26.             }  
  27.         }  
  28.     }  
  29. }  
The output looks like the following:
 
remove.jpg
 

Summary

 
In this tutorial, I've explained how to create a SortedList in C# and use its methods and properties.
 
Note
If you're new to collections in C#, here is a must read article: Collections in C# 


Similar Articles