Recipe 9.10 Sorting a Hashtable'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
Hashtable object to obtain an
ICollection of its key
and value objects. The methods shown here return an
ArrayList of objects
containing the keys or values of a
Hashtable:
using System;
using System.Collections;
// Return an ArrayList of Hashtable keys
public static ArrayList GetKeys(Hashtable table)
{
return (new ArrayList(table.Keys));
}
// Return an ArrayList of Hashtable values
public static ArrayList GetValues(Hashtable table)
{
return (new ArrayList(table.Values));
}
The following code creates a Hashtable object and
displays first keys, and then values, sorted in ascending and
descending order:
public static void TestSortKeyValues( )
{
// Define a hashtable object
Hashtable hash = new Hashtable( );
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 hashtable and sort them
ArrayList keys = GetKeys(hash);
keys.Sort( );
// Display sorted key list
foreach (object obj in keys)
Console.WriteLine("Key: " + obj + " Value: " + hash[obj]);
// Reverse the sorted key list
Console.WriteLine( );
keys.Reverse( );
// Display reversed key list
foreach (object obj in keys)
Console.WriteLine("Key: " + obj + " Value: " + hash[obj]);
// Get all the values in the hashtable and sort them
Console.WriteLine( );
Console.WriteLine( );
ArrayList values = GetValues(hash);
values.Sort( );
foreach (object obj in values)
Console.WriteLine("Value: " + obj);
// Reverse the sorted value list
Console.WriteLine( );
values.Reverse( );
foreach (object 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 Hashtable
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 Hashtable. The Values
property returns the same for all values currently contained in the
Hashtable.
The GetKeys method uses the
Keys property. Once the
ICollection of keys is returned through this
property, a new ArrayList is created to hold the
keys. This ArrayList is then returned to the
caller. The GetValues method works in a similar manner except that it
uses the Values property.
The ICollection object returned either from the
Keys or Values properties of a
Hashtable object are direct references to the key
and value collections within the Hashtable. This
means that if the keys and/or values change in a
Hashtable, the key and value collections will be
altered accordingly.
See Also
See the "Hashtable Class" and
"ArrayList Class" topics in the
MSDN documentation.
|