Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.

If you own the book already, login here to get free, online, searchable access to the entire book's content.

If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".

7.14 Sort a Hashtable by Key or Value

Problem

You have a hashtable of keys and values, and you want to get the list of values that result from sorting the keys in order.

Solution

To sort a hashtable, use the GetEnumerator() method on the hashtable to access its individual elements. Then use the Sort-Object cmdlet to sort by Name or Value:

foreach($item in $myHashtable.GetEnumerator() | Sort-Object Name)
{
    $item.Value
}

If you control the definition of the hashtable, use the [Ordered] type cast while defining the hashtable to have it retain the order supplied in the definition.

$orderedHashtable = [Ordered] @{ Item1 = "Hello"; Item2 = "World" }

Discussion

Since the primary focus of a hashtable is to simply map keys to values, it doesn’t usually retain any ordering whatsoever—such as the order you added the items, the sorted order of the keys, or the sorted order of the values. This becomes clear in Example 7-3.

Example 7-3. A demonstration of hashtable items not retaining their order
PS > $myHashtable = @{}
PS > $myHashtable["Hello"] = 3
PS > $myHashtable["Ali"] = 2
PS > $myHashtable["Alien"] = 4
PS > $myHashtable["Duck"] = 1
PS > $myHashtable["Hectic"] = 11
PS > $myHashtable

Name                           Value
----                           -----
Hectic                         11
Duck                           1
Alien                          4
Hello                          3
Ali                            2

However, the hashtable object supports a GetEnumerator() method that lets you deal with the individual hashtable entries—all of which have a Name and Value property. Once you have those, we can sort by them as easily as we can sort any other PowerShell data. Example 7-4 demonstrates this technique.

Example 7-4. Sorting a hashtable by name and value
PS > $myHashtable.GetEnumerator() | Sort-Object Name

Name                           Value
----                           -----
Ali                            2
Alien                          4
Duck                           1
Hectic                         11
Hello                          3

PS > $myHashtable.GetEnumerator() | Sort-Object Value

Name                           Value
----                           -----
Duck                           1
Ali                            2
Hello                          3
Alien                          4
Hectic                         11

By using the [Ordered] type cast, you can create a hashtable that retains the order in which you define and add items:

PS > $myHashtable = [Ordered] @{
    Duck = 1;
    Ali = 2;
    Hectic = 11;
    Alien = 4;
 }

PS > $myHashtable["Hello"] = 3
PS > $myHashtable

Name                           Value
----                           -----
Duck                           1
Ali                            2
Hectic                         11
Alien                          4
Hello                          3

For more information about working with hashtables, see “Hashtables (Associative Arrays)”.

See Also

“Hashtables (Associative Arrays)”