How To Use Sorted Lists In C# – Made Easy

Sorted lists in c# are lists that will automatically sort its elements. This guide explains how to use them and what methods are available.


Andraz Krzisnik
How To Use Sorted Lists In C# – Made...

Sorted lists are a special kind of list type, which automatically sorts the elements it holds. Furthermore, we’re going to take a look at how they work in C#.

In case you want to better understand lists in C#, I suggest you take a look at other posts about them on this blog. So far I’ve covered generic lists and array lists, which can serve as an introduction to lists in general.

Sorted lists in C#

Now, let’s go more in depth in the general workings of sorted lists. First of all, we can find generic class SortedList from System.Collections.Generic namespace. This is the class we’re going to call whenever we’re going to declare a new sorted list.

There is one important aspect of these type of lists and it’s that it requires to store elements in data type pairs. To be more accurate, these lists are collections of key-value pairs. However, they don’t come without any constraints.

For one, they are sorted by the keys. Therefore, we can’t use keys that are equal to null. And another thing we need to watch out for is, that all keys in a list are unique.

Now, let’s see some examples in code, so we can see what’s even possible.

SortedList<string, int> phonebook = new SortedList<string, int>();

Console.WriteLine("People in phonebook: ");
phonebook.Add("Marcel", 645645645);
phonebook.Add("Tara", 827878787);
phonebook.Add("Benjamin", 232323233);
phonebook.Add("Sabine", 767676776);

foreach (KeyValuePair<string, int> contact in phonebook)
{
    Console.WriteLine($"{contact.Key} : {contact.Value}");
}
Console.WriteLine(Environment.NewLine);

phonebook.Remove("Marcel");
Console.WriteLine("People in phonebook: ");

foreach (KeyValuePair<string, int> contact in phonebook)
{
    Console.WriteLine($"{contact.Key} : {contact.Value}");
}
Console.WriteLine(Environment.NewLine);

Console.WriteLine("Search for contact: ");
string search = Console.ReadLine();
if (phonebook.ContainsKey(search))
{
    Console.WriteLine("Found contact: " + search + " at index: " + phonebook.IndexOfKey(search));
}
else
{
    Console.WriteLine("Contact " + search + " doesn't exist.");
}

As you can see, I coded a simple phonebook example using SortedList class and it’s methods. I also want to mention that there are other methods that I didn’t use in this example. For example, we can also use ContainsValue and IndexOfValue and I suspect that you can already imagine what they do.

We can declare sorted lists with various different data type for key and value pairs. You could also use custom object classes in case you want to fit more info for each element.

Conclusion

I hope this short guide helped you gain a better understanding about sorted lists and how to use them.

You can also download the demo project I made for this tutorial and try it out yourself.

Show Comments (0)

Comments

Related Articles

Frequency Domain Filtering

How To Use Notch Filters – C# Guide

We can use notch filters for attenuating frequencies on custom locations across the frequency map. But for that we will need to utilize all of the knowledge we’ve acquired...

Posted on by Andraz Krzisnik
Order-Statistic Filters

How To Make Max And Min Filter In Image Processing

Max and min filter is one of the order-statistic filter we can use to process image data in spatial domain. This tutorial shows it with C#.

Posted on by Andraz Krzisnik