PowerShell Sorted Dictionaries

A sorted dictionary in PowerShell is very similar to a dictionary, as it stores data in key and value pairs. The key difference, however, is the order of the key and value pairs. Whilst a dictionary stores its contents in the order that it was added, a sorted dictionary stores its key and value pairs in ascending order of the key.

Below is an example of a sorted dictionary, called ‘people’, where the key is a string, that holds a person’s name and the value is an integer for their age. The 'Out-Null' cmdlet is used to suppress unwanted output in the terminal.

$people = [System.Collections.Generic.SortedDictionary[string,int]]::new()
$people.Add("Bob Smith", 30) | Out-Null
$people.Add("George Jones", 21) | Out-Null
$people.Add("Fred Bloggs", 43) | Out-Null
$people.Add("Alan White", 29) | Out-Null

The first line declares the sorted dictionary, with a string for the key and an integer for the value. Four key and value pairs are then added to the sorted dictionary using its ‘Add’ method, with a person’s name as the key and their age as the value.

It is possible to see how many key and value pairs there are in the sorted dictionary using the ‘Count’ property of the sorted dictionary itself.

Write-Host $people.Count

As with dictionaries and other collections, it is possible to loop through a sorted dictionary using a ‘foreach’ loop. Here, the key and value for each person are incorporated into a sentence stating a person’s age.

foreach ($person in $people.GetEnumerator())
{

    Write-Host "$($person.Key) is $($person.Value) years old."

}

The output in the terminal from the above example is as follows.

Alan White is 29 years old.
Bob Smith is 30 years old.
Fred Bloggs is 43 years old.
George Jones is 21 years old.

It is also possible to access a single value if the key is known.

Write-Host $people["Bob Smith"]

Sorted dictionaries share the flexibility of lists with the ease of adding, updating and removing key and value pairs. Here a key of ‘John Smith’, with a value of ‘52’ is added to the ‘people’ sorted dictionary.

$people.Add("John Smith", 52) | Out-Null

Note that if the key ‘John Smith’ already exists, then an error will be produced. To stop this from happening the ‘ContainsKey’ sorted dictionary method could be used to check to see if the key already exists before adding it.

if (-not ($people.ContainsKey("John Smith")))
{

    $people.Add("John Smith", 52) | Out-Null

}

To update a value in a sorted dictionary, the sorted dictionary name and key need to be specified.

$people["Bob Smith"] = 100

Note that if the key specified doesn’t already exist, then this has the same effect as using the sorted dictionary ‘Add’ method, where a new key and value pair are added to the sorted dictionary.

Removing a key and value pair from a sorted dictionary is simply a matter of using the ‘Remove’ method and specifying the key.

$people.Remove("George Jones") | Out-Null

Finally, if it is necessary to remove all items in a sorted dictionary in one go, instead of individually with the ‘Remove’ method, there is a ‘Clear’ method to accomplish this.

$people.Clear()