How to sort the console output of a hashtable in PowerShell

Published 10 July 07 12:01 AM | andarno 

Hashtables are inherently unsorted, but when you're printing a hashtable's contents to the console, it can certainly be helpful to sort its contents by key.  Although it's not obvious, the way to do it is pretty easy.

Let's start with defining a hashtable and play with it until it's obviously unsorted.

PS C:\> $h = @{b=2;a=3;c=1}
PS C:\> $h

Name                           Value
----                           -----
a                              3
b                              2
c                              1


PS C:\> $h['d']=5
PS C:\> $h

Name                           Value
----                           -----
a                              3
b                              2
d                              5
c                              1

Great. Now let's sort it:

PS C:\> $h | sort name

Name                           Value
----                           -----
a                              3
b                              2
d                              5
c                              1

Ergh! What happened? Well, without going into tons of detail, it has to do with the type that backs HashTable and how you can't sort the object itself. You've got to generate a list of key-value pairs and sort that.

Here's the correct way to do it:

PS C:\> $h.GetEnumerator() | sort name

Name                           Value
----                           -----
a                              3
b                              2
c                              1
d                              5

Great. And yes, sorting by value works too:

PS C:\> $h.GetEnumerator() | sort value

Name                           Value
----                           -----
c                              1
b                              2
a                              3
d                              5
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# /\/\o\/\/ said on July 10, 2007 5:00 AM:

An other way to sort by key is casting it into a sortedlist.

[collections.sortedlist]$h

Greetings /\/\o\/\/

# Squawks said on July 17, 2007 10:16 AM:

Excellent post! I'm new at this Powershell stuff and ran across this problem recently. I never did find a solution - until now. Thanks for posting this!

# Alex K. Angelopoulos said on September 21, 2007 11:37 AM:

Glad I saw this, Andrew. I've been casting hashes to [System.Collections.SortedList] and hadn't even realized that I could use the enumerator as well.

# Jay said on June 18, 2009 4:47 AM:

I have a hash table where the value contains date stores as string. How do I sort the hash table on the values (however I want the sorting to be done by date and not date as string).

# Hal Rottenberg said on October 12, 2009 12:13 PM:

Always nice to find a tip wrapped up so well in a blog post. You helped me out today, Andrew, thanks!

# Hal Rottenberg said on October 12, 2009 12:17 PM:

Interesting, casting to a SortedList is 10x faster.

§ HXRLAP2 {~} $sb1 = { 1..100 | % { $a.GetEnumerator() | sort Name } }

§ HXRLAP2 {~} $sb2 = { 1..100 | % { [collections.SortedList]$ae } }

§ HXRLAP2 {~} Measure-Command $sb1 | Measure-Object -Property milliseconds -Average

Count    : 1

Average  : 135

Sum      :

Maximum  :

Minimum  :

Property : Milliseconds

§ HXRLAP2 {~} Measure-Command $sb2 | Measure-Object -Property milliseconds -Average

Count    : 1

Average  : 16

Sum      :

Maximum  :

Minimum  :

Property : Milliseconds

# June Blender said on October 12, 2009 12:27 PM:

Great post. This method is included in the latest version of about_hash_tables (http://go.microsoft.com/fwlink/?LinkID=135175), updated 8/19/2009.

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Interesting blogs

Related sites

Syndication

Page view tracker