Working on SortedList Using C#

Introduction

The SortedList<> class represents a collection of key/value pairs that are sorted by key. The hierarchy of SortedList is System.Collections.Generic.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 the SortedList and the various mathods and properties of SortedList.

Creation of SortedList

We can create the SortedList by the following line of code:

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


Add elements in SortedList

We can add the elements by using the add() method. For example:

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            //display the elements of the sortedlist

            Console.WriteLine("The elements in the SortedList are:");

            foreach (KeyValuePair<int, string> pair in sortedlist)

            {

                Console.WriteLine("{0} => {1}", pair.Key, pair.Value);

            }

        }

    }

}

Output

sortedlist1.jpg

Properties

Capacity : Gets or sets the number of elements that the SortedList<TKey, TValue> can contain. The bydefault value of capacity is 4, which allows for 4 elements to be added to the list, and if we enter the 5th element then the capacity increases by 4, which means if there are 5 elements in the list then the capacity will be 8. In the following example we add an element with the number 7 so that the capacity will be 8; if we add 9 elements then the capacity will be 16 (double the previous one).

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            //display the elements of the sortedlist

            Console.WriteLine("The elements in the SortedList are:");

            foreach (KeyValuePair<int, string> pair in sortedlist)

            {

                Console.WriteLine("{0} => {1}", pair.Key, pair.Value);

            }

            //Find the capacity

            Console.WriteLine("The capacity is:" + sortedlist.Capacity);

            sortedlist.Add(8, "januray");

            sortedlist.Add(9, "april");

            Console.WriteLine("After adding two more element the capacity is:" + sortedlist.Capacity);

        }

    }

}

Output

sortedlist2.jpg

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:

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            //display the elements of the sortedlist

            Console.WriteLine("The elements in the SortedList are:");

            foreach (KeyValuePair<int, string> pair in sortedlist)

            {

                Console.WriteLine("{0} => {1}", pair.Key, pair.Value);

            }

            Console.WriteLine("The total number of elements in the sortedlist are:" + sortedlist.Count);

            sortedlist.Add(8, "januray");

            sortedlist.Add(9, "april");

            Console.WriteLine("After adding two more element the number of element in sortedlist are:" + sortedlist.Count);

        }

    }

}

 

Output

sortedlist3.jpg


Difference between Capacity and Count

difference.jpg


Keys :
It gets a collection containing the keys in the SortedList<TKey, TValue>; for this we use IList, for example:
 

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            //display the elements of the sortedlist

            Console.WriteLine("The elements in the SortedList are:");

            foreach (KeyValuePair<int, string> pair in sortedlist)

            {

                Console.WriteLine("{0} => {1}", pair.Key, pair.Value);

            }

            IList<int> ilistKeys = sortedlist.Keys;

            Console.WriteLine();

            Console.WriteLine("The keys are:");

            Console.Write("{");

            foreach (int i in ilistKeys)

            {

                Console.Write(i + ",");

            }

            Console.WriteLine("}");

        }

    }

}

 

Output

sortedlist4.jpg

Values : Gets a collection containing the values in the SortedList<TKey, TValue>. For example:

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            //display the elements of the sortedlist

            Console.WriteLine("The elements in the SortedList are:");

            foreach (KeyValuePair<int, string> pair in sortedlist)

            {

                Console.WriteLine("{0} => {1}", pair.Key, pair.Value);

            }

            IList<string> ilistvalues = sortedlist.Values;

            Console.WriteLine();

            Console.WriteLine("The Values are:");

            Console.WriteLine();

            Console.Write("{");

            foreach (string i in ilistvalues)

            {

                Console.Write(i + ",");

            }

            Console.WriteLine("}");

        }

    }

}

 

Output

sortedlist5.jpg

 

Methods

 

ContainsKey() : Determines whether the SortedList<TKey, TValue> contains a specific key. For example:

 

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            //display the elements of the sortedlist

            Console.WriteLine("The elements in the SortedList are:");

            foreach (KeyValuePair<int, string> pair in sortedlist)

            {

                Console.WriteLine("{0} => {1}", pair.Key, pair.Value);

            }

            //apply contain key method

            Console.WriteLine("The key 1 contain in the SortedList:" + sortedlist.ContainsKey(1));

            Console.WriteLine("The key 5 contain in the SortedList:" + sortedlist.ContainsKey(5));

            Console.WriteLine("The key 10 contain in the SortedList:" + sortedlist.ContainsKey(10));

            Console.WriteLine("The key 50 contain in the SortedList:" + sortedlist.ContainsKey(50));

        }

    }

}

 

Output

containkey.jpg

 

ContainsValue() : Determines whether the SortedList<TKey, TValue> contains a specific value. For example:

 

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            //display the elements of the sortedlist

            Console.WriteLine("The elements in the SortedList are:");

            foreach (KeyValuePair<int, string> pair in sortedlist)

            {

                Console.WriteLine("{0} => {1}", pair.Key, pair.Value);

            }

            //apply contain value method

            Console.WriteLine("The value Sunday contain in the SortedList:" + sortedlist.ContainsValue("Sunday"));

            Console.WriteLine("The value Friday contain in the SortedList:" + sortedlist.ContainsValue("Friday"));

            Console.WriteLine("The value May contain in the SortedList:" + sortedlist.ContainsValue("May"));

            Console.WriteLine("The value Wednesday contain in the SortedList:" + sortedlist.ContainsValue("Wednesday"));

        }

    }

}
 

Output

containvalue.jpg

Clear() : Removes all elements from the SortedList<TKey, TValue>. For example:
 

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            Console.WriteLine("The number of elements in the sortedlist are:" + sortedlist.Count);

            //apply clear method

            sortedlist.Clear();

            Console.WriteLine("After clear method the elements in the sortedlist are:" + sortedlist.Count);

        }

    }

}

 

Output

clear.jpg

 

IndexOfKey() : Searches the specified key and returns the index value of that key.

IndexOfValue() : Searches the specified value and returns the index value of that value.
 

The following example shows the use of both IndexOfKey and IndexOfValue methods

 

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            //IndexOfKey method

            Console.WriteLine("***************INDEXOFKEY***************");

            Console.WriteLine();

            Console.WriteLine("The index value of the key 4 is:" + sortedlist.IndexOfKey(4));

            Console.WriteLine("The index value of the key 1 is:" + sortedlist.IndexOfKey(1));

            Console.WriteLine("The index value of the key 7 is:" + sortedlist.IndexOfKey(7));

            Console.WriteLine("The index value of the key 2 is:" + sortedlist.IndexOfKey(2));

            //IndexofValue method

            Console.WriteLine();

            Console.WriteLine("***************INDEXOFVALUE***************");

            Console.WriteLine();

            Console.WriteLine("The index value of the value Sunday is:" + sortedlist.IndexOfValue("Sunday"));

            Console.WriteLine("The index value of the value Wednesday is:" + sortedlist.IndexOfValue("Wednesday"));

            Console.WriteLine("The index value of the value Monday is:" + sortedlist.IndexOfValue("Monday"));

            Console.WriteLine("The index value of the value Friday is:" + sortedlist.IndexOfValue("Friday"));

        }

    }

}

 

Output

indexofkey.jpg

 

Remove() : Removes the element with the specified key from the SortedList<TKey, TValue>. For example:

namespace SortedList

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of sortedlist

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

            //add the elements in sortedlist

            sortedlist.Add(1, "Sunday");

            sortedlist.Add(2, "Monday");

            sortedlist.Add(3, "Tuesday");

            sortedlist.Add(4, "Wednesday");

            sortedlist.Add(5, "Thusday");

            sortedlist.Add(6, "Friday");

            sortedlist.Add(7, "Saturday");

            //display the elements of the sortedlist

            Console.WriteLine("The elements in the SortedList are:");

            foreach (KeyValuePair<int, string> pair in sortedlist)

            {

                Console.WriteLine("{0} => {1}", pair.Key, pair.Value);

            }

            //remove the elements from the sortedlist

            sortedlist.Remove(3);

            sortedlist.Remove(7);

            sortedlist.Remove(1);

            Console.WriteLine("After remove some elements the sortedlist is as:");

            foreach (KeyValuePair<int, string> pair1 in sortedlist)

            {

                Console.WriteLine("{0} => {1}", pair1.Key, pair1.Value);

            }

        }

    }

}

 

Output

remove.jpg

Summary
 

In this article I explained how to create the SortedList in C# and the various methods and properties of SortedList.

Up Next
    Ebook Download
    View all
    Learn
    View all