I am here to continue the series related to .NET interview preparation. Today, we will discuss the common questions related to the sorted collections and present the answers in an easy way.
Link to previous posts,
So let’s take questions one by one,
1. What are sorted collections in .NET?
The sorted collections are the collections used to provide the built-in sorting capability. Following are the sorted collections available in .NET.
2. What are the similarities and the differences between Sorted Dictionary and Sorted List? Also, provide the uses scenarios.
Both SortedDictionary and SortedList are used in the cases when sorting is required to happen automatically based on the keys. However, they have some differences in the uses and performance as follows.
To summarize, SortedDictionary<K, V> should be used, when-
Sotted List<K, V> should be used, when-
3. How to use generic SortedList. Explain with example.
Generic SortedList is similar to the list with the difference that it automatically keeps the data sorted by the keys.
Let’s understand SortedList by a simple example.
  1. SortedList<int, string>mySortedList = newSortedList<int, string>();
  2. mySortedList.Add(102, "Prakash");
  3. mySortedList.Add(101, "Aradhana");
  4. mySortedList.Add(104, "Beeda");
  5. mySortedList.Add(103, "Satna");
  6. mySortedList.Add(105, "Amarpatan");
  7. //Get the index by passing key
  8. intfirstKey = mySortedList.Keys.First();
  9. intfirstIndex = mySortedList.IndexOfKey(firstKey);
  10. Console.WriteLine("Index of key {0} is: {1}", firstKey, firstIndex);
  11. //Get the value by passing the index
  12. Console.WriteLine("Value of index {0} is: {1}", firstIndex, mySortedList.Values[firstIndex]);
  13. //Get the value by key as default indexer is implemented using key
  14. Console.WriteLine("Value of key {0} is: {1}", firstKey, mySortedList[firstKey]);
  15. Console.WriteLine("\nPrinting the SortedList:");
  16. foreach (varkvpinmySortedList)
  17. {
  18. Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
  19. }
Output
As you can see, that although we have entered the keys in a random order SortedList has automatically adjusted the sort order. You can also see that SortedList exposes the index property apart from the keys and values.
4. How to use SortedDictionary. Explain with example.
SortedDictionary works similar to a dictionary except that it automatically keeps the data sorted by the keys.
Let’s understand SortedDictionary by a simple example.
  1. SortedDictionary < int, string > myDict = newSortedDictionary < int, string > ();
  2. myDict.Add(102, "Prakash");
  3. myDict.Add(101, "Aradhana");
  4. myDict.Add(104, "Beeda");
  5. myDict.Add(103, "Satna");
  6. myDict.Add(105, "Amarpatan");

  7. //Get the index by passing key

  8. intfirstKey = myDict.Keys.First();
  9. //intfirstIndex = myDict.IndexOfKey(firstKey); //Error as SortedDictionary can't be accessed by index
  10. //Get the value by key as default indexer is implemented using key
  11. Console.WriteLine("Value of key {0} is: {1}", firstKey, myDict[firstKey]);
  12. Console.WriteLine("\nPrinting the SortedDictionary:");
  13. foreach(varkvpinmyDict)
  14. {
  15. Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
  16. }
Output
5. How to use SortedSet. Explain with example.
SortedSet is similar to HashSet with an additional built-in sorting functionality. HashSet is a unique non-ordered collection, optimized for lookups and SortedSet is one step, ahead with the support of sorting.
Let’s understand SortedSet by a simple example.
  1. SortedSet < string > mySet = newSortedSet < string > ();
  2. mySet.Add("Prakash");
  3. mySet.Add("Aradhana");
  4. mySet.Add("Beeda");
  5. mySet.Add("Satna");
  6. mySet.Add("Prakash");
  7. mySet.Add("Beeda");
  8. mySet.Add("Amarpatan");
  9. Console.WriteLine("Printing the SortedSet:");
  10. foreach(var value inmySet)
  11. {
  12. Console.WriteLine(value);
  13. }
  14. mySet.Remove("Prakash");
  15. Console.WriteLine("\nPrinting the SortedSet After Removing an element:");
  16. foreach(var value inmySet)
  17. {
  18. Console.WriteLine(value);
  19. }
Output
As you can see in the output:
You can also download the attached demo project (SortedCollectionsDemo.zip) to go through the source code used in the article.