SortedList Class:
The SortedList Class represents a Collection of Key and Value pairs that are Sorted by The Keys and Are Accessible by Key or by Index. The SortedList Collection Stores Key and Value Pairs in the Ascending Order of Key by Default.
A Sorted List is a Combination of Array and a Hash Table. It contains a list of Items that can be accessed using a Key or and Index. if we Access items using an Index, it is ArrayList, and If We Access Items using a Key, It is Hashtable. The Collection of Items is Always Sorted By the Key Value.
C# SortedList Class Provides some Properties and Methods to work with SortedList Collection. The Following tables lists some of the Commonly used Properties of the SortedList Class:
Properties | Description |
---|---|
Capacity | Gets or sets the capacity of a SortedList object. |
Count | Gets the number of elements contained in a SortedList object. |
IsFixedSize | Gets a value indicating whether a SortedList object has a fixed size. |
IsReadOnly | Gets a value indicating whether a SortedList object is read-only. |
item | Gets and sets the value associated with a specific key in a SortedList object. |
Keys | Gets the keys in a SortedList object. |
Values | Gets the values in a SortedList object. |
The Following tables lists some of the Commonly Used Methods of the SortedList Class:
Methods | Description |
---|---|
Add() | Adds an element with the specified key and value to a SortedList object. |
Clear() | Removes all elements from a SortedList object. |
Contains | Determines whether a SortedList object contains a specific key. |
GetByIndex() | Gets the value at the specified index of a SortedList object. |
GetKey() | Gets the key at the specified index of a SortedList object. |
GetKeyList() | Gets the keys in a SortedList object. |
GetValueList() | Gets the values in a SortedList object. |
IndexOfKey() | Returns the zero-based index of the specified key in a SortedList object. |
IndexOfValue() | Returns the zero-based index of the first occurrence of the specified value in a SortedList object. |
Remove() | Removes the element with the specified key from a SortedList object. |
RemoveAt() | Removes the element at the specified index of a SortedList object. |
SetByIndex() | Replaces the value at a specific index in a SortedList object. |
TrimToSize() | Sets the capacity to the actual number of elements in a SortedList object. |
For complete list of properties and methods, please visit Microsoft's C# documentation. |
Let's have an Example to clear the concept about SortedList. In this example, we will add data in sortedList randomly, and we will see if our List can sort entered data in ascending order.
/*Example - SortedList - InfoBrother*/
using System;
using System.Collections;
public class MySortedList
{
public static void Main()
{
// Creates new SortedList.
SortedList SList = new SortedList();
//Add some Elements in list:
SList.Add(1255, "Hina");
SList.Add(1258, "Usman");
SList.Add(1257, "Kinat");
SList.Add(1260, "Khizar");
//Get the Total Number of Elements:
Console.WriteLine(" Count: {0}", SList.Count);
//Get the Capacity of List:
Console.WriteLine(" Capacity: {0}", SList.Capacity);
Console.WriteLine(" Roll # and Student Name:");
PrintKeysAndValues(SList); //Print record:
//add some more values:
SList.Add(1256, "Aisha");
SList.Add(1259, "Taimur");
Console.WriteLine("\nUpdated Student Record:");
PrintKeysAndValues(SList); //Print record:
//Get the Total Number of Elements:
Console.WriteLine(" Count: {0}", SList.Count);
//Remove student whose ID # is 1256.
Console.WriteLine("\n Remove Student Having ID #1256");
SList.Remove(1256);
PrintKeysAndValues(SList); //Print record:
//remove student from index # 1. i.e element # 2:
Console.WriteLine("\n Remove Student From Index #1:");
SList.RemoveAt(1);
PrintKeysAndValues(SList); //Print record:
//Check for Already Existing Names:
Console.WriteLine("\nDo We have Student ID #1225? " + SList.Contains(1225));
Console.WriteLine("Do We have Student ID #1259? " + SList.Contains(1259));
Console.ReadKey();
}
//Method to display record:
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" Roll# Name");
//Access values using for loop:
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0}: {1}", myList.GetKey(i), myList.GetByIndex(i));
}
}
}
SortedList Class:
Count: 4 Capacity: 16 Roll # and Student Name: Roll# Name 1255: Hina 1257: Kinat 1258: Usman 1260: Khizar Updated Student Record: Roll# Name 1255: Hina 1256: Aisha 1257: Kinat 1258: Usman 1259: Taimur 1260: Khizar Count: 6 Remove Student Having ID #1256 Roll# Name 1255: Hina 1257: Kinat 1258: Usman 1259: Taimur 1260: Khizar Remove Student From Index #1: Roll# Name 1255: Hina 1258: Usman 1259: Taimur 1260: Khizar Do We have Student ID #1225? False Do We have Student ID #1259? True