BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

The SortedList Collection

The thirty-ninth part of the C# Fundamentals tutorial describes the use of the SortedList class. This dictionary collection provides a hybrid of the Hashtable and Array types; each entry being a key / value pair sorted according to the key's contents.

Updating Using the Index Number

It is possible to access the items in a sorted list using their index numbers rather than using keys. Unlike an array, you cannot provide the index number in square brackets; this would cause conflicts between index numbers and numeric keys. To use the index number to change the value of an existing item, the SetByIndex method is called. The method has two parameters: the first is the index number of the entry to be updated, the second is the new value.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

veg.SetByIndex(0, "Late Flat Dutch");       // Item updated

NB: Remember that the index number is not related to the order in which items were added to the collection. It is based upon the positions of the items after sorting. In the above example the update only works because we know that 'cabbage' is the first key when sorted alphabetically.

Removing an Item

SortedList permits items to be removed using the Remove method. This removes an item from the collection that matches a specified key. The SortedList also provides an additional method that removes an item based upon its index number. This is the RemoveAt method.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

veg.RemoveAt(0);                            // Item removed

Reading SortedList Information

Retrieving an Item by Index

The SortedList collection is a dictionary and, as such, permits an item to be read using a key as a reference. In addition, you can access SortedList items by index number using the GetByIndex method.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

Console.WriteLine(veg.GetByIndex(0));       // Outputs "Savoy"

Retrieving a Key by Index

In addition to being able to retrieve a value from a SortedList by index number, the key may also be found in this manner. The GetKey method uses a similar syntax to GetByIndex, requiring an integer parameter holding the index number to look up.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

Console.WriteLine(veg.GetKey(0));           // Outputs "cabbage"

Iterating Using DictionaryEntry Objects

When an item is added to a SortedList, the key and the value elements are combined into a single DictionaryEntry object. Using the foreach statement, it is possible to iterate through each item and extract the key and value from the DictionaryEntry object. The following example outputs the details of each key / value pair to the console.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

foreach(DictionaryEntry de in veg)
{
    Console.WriteLine("{0}\t: {1}", de.Key, de.Value);
}

/* OUTPUT

cabbage : Savoy
carrot  : Imperator
lettuce : Iceberg
potato  : King Edward

*/

Values

As with Hashtables, SortedLists include a Values property that returns a collection of the values in the dictionary:

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

foreach(string value in veg.Values)
{
    Console.WriteLine(value);
}

/* OUTPUT

Savoy
Imperator
Iceberg
King Edward

*/
29 May 2007