Sorting Hash Tables

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'; Age=66; Status='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

Posted Nov 13 2008, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.