Sorting a Dictionary's Keys and/or Values




Sorting a Dictionary's Keys and/or Values

Problem

You want to sort the keys and/or values contained in a Hashtable in order to display the entire Hashtable to the user, sorted in either ascending or descending order.

Solution

Use the Keys and Values properties of a Dictionary<T,U> object to obtain an ICollection of its key and value objects. The methods shown here return a List<T> of objects containing the keys or values of a Dictionary<T,U>:

	using System;
	using System.Collections;

	using System.Collections.Generic;

	public static List<T> GetKeys<T,U>(Dictionary<T,U> table)
	{
	    return (new List<T>(table.Keys));
	}

The method shown here returns a List<U> of objects containing the values in a Dictionary<T,U>:

	using System;
	using System.Collections;
	using System.Collections.Generic;

	public static List<U> GetValues<T,U>(Dictionary<T,U> table)
	{
	    return (new List<U>(table.Values));
	}

The following code creates a Dictionary<T,U> object and displays it sorted in ascending and descending order:

	public static void TestSortKeyValues()
	{
	    // Define a Dictionary<T,U> object.
	    Dictionary<string, string> hash = new Dictionary<string, string>();
	    hash.Add(2, "two");
	    hash.Add(1, "one");
	    hash.Add(5, "five");
	    hash.Add(4, "four");
	    hash.Add(3, "three");

	    // Get all the keys in the Dictionary<T,U> and sort them.
	    List<string> keys = GetKeys(hash);
	    keys.Sort( );

	    // Display sorted list.
	    foreach (object obj in keys)
	        Console.WriteLine("Key: " + obj + " Value: " + hash[obj]);

	    // Reverse the sorted list.
	    Console.WriteLine( );
	    keys.Reverse( );

	    // Display reversed list.
	    foreach (object obj in keys)
	        Console.WriteLine("Key: " + obj + " Value: " + hash[obj]);
	    Console.WriteLine( );
	    Console.WriteLine( );

	    // Get all the values in the Dictionary<T,U> and sort them.
	    List<string> values = GetValues(hash);
	    values.Sort( );

	    // Display sorted list.
	    foreach (string obj in values)
	        Console.WriteLine("Value: " + obj);

	    // Reverse the sorted value list.
	    Console.WriteLine( );
	    values.Reverse( );

	    // Display sorted list.
	    foreach (string obj in values)
	        Console.WriteLine("Value: " + obj);}

The key/value pairs are displayed as shown:

	Key: 1 Value: one
	Key: 2 Value: two
	Key: 3 Value: three
	Key: 4 Value: four
	Key: 5 Value: five

	Key: 5 Value: five
	Key: 4 Value: four
	Key: 3 Value: three
	Key: 2 Value: two
	Key: 1 Value: one

	Value: five      Notice that the values are sorted alphabetically
	Value: four
	Value: one
	Value: three
	Value: two

	Value: two
	Value: three
	Value: one
	Value: four
	Value: five

Discussion

The Dictionary<T,U> object exposes two useful properties for obtaining a collection of its keys or values. The Keys property returns an ICollection containing all the keys currently in the Dictionary<T,U>. The Values property returns the same for all values currently contained in the Dictionary<T,U>.

The GetKeys method uses the Keys property. Once the ICollection of keys is returned through this property, a new List<T> is created to hold the keys. This List<T> is then returned to the caller. The GetValues method works in a similar manner except that it uses the Values property.

The GetValues method uses the Values property. Once the ICollection of values is returned through this property, a new List<U> is created of the same size to hold the values. This List<U> is then returned to the caller.

The ICollection object returned from either the Keys or Values property of a Dictionary<T,U> object contains direct references to the key and value collections within the Dictionary<T,U>. This means that if the keys and/or values change in a Dictionary<T,U>, the key and value collections will be altered accordingly.

See Also

See the "Dictionary<T,U> Class" and "List<T> Class" topics in the MSDN documentation.