Sorting Hash Tables

by Nov 13, 2008

Hash Tables store key-value pairs, and you normally cannot sort its content. Let's define a Hash Table first to examine this:

$hash = @{Name='Tobias'=='Online'}

When you output its content, you get two columns, key and value, and when you pipe the result into Sort-Object, the result is not sorted:

$hash | Sort-Object Name

To sort a Hash Table, you need to transform its content into individual objects that can be sorted by using GetEnumerator():

$hash.GetEnumerator() | Sort-Object Name