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.
Filter by Category
Sorted lists in c# are lists that will automatically sort its elements. This guide explains how to use them and what methods are available.
Generic lists in C# are a data structures that allow us to add and remove objects to store inside without declaring its size.
Array lists are one of the simplest varieties of lists that C# libraries have to offer and this tutorial explains how to use them.
Quicksort algorithm or divide and conquer algorithm is one of the most used sorting algorithms, because of its supperior efficiency.
Bubble sort is one of the simplest sorting algorithms and this guide shows how to implement it in C# programming language.
Insertion sort is one of the simplest sorting algorithms for sorting single dimensional arrays of various different data types.
Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.
C# arrays are data structures for storing multiple items of the same type. This guide shows you how to use different varieties of arrays.
SLIC superpixel segmentation is a modern operation for reducing irrelevant detail for shortening computational time in further processing.
K means clustering is a optimization method of partitioning an image by measuring Euclidean distances between pixels and cluster means.
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.
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.
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.