Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Sorting a hashtable

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-13-2006   #1 (permalink)
johnjhamm@gmail.com
Guest


 

Sorting a hashtable

I wrote this command to show the size of all sub-folders in a folder:

dir | ? { $_.PsIsContainer } | % { $t=0;dir -rec $_.Name | % {$t +=
$_.Length }; @{$_.Name=$t}}

This outputs:

Name Value
---- -----
Avaya 1887047
C# 294
PowerShell 8643
Regular Expressions 161
Sql 3914
Windows 1964032


Now, I'd like to sort them by size (or the "Value" property of the
Hashtable). I append "sort value" to the end of my command:

dir | ? { $_.PsIsContainer } | % { $t=0;dir -rec $_.Name | % {$t +=
$_.Length }; @{$_.Name=$t}} | sort value

But I get:

Name Value
---- -----
Regular Expressions 161
Sql 3914
Windows 1964032
Avaya 1887047
C# 294
PowerShell 8643


How do I get this to sort?!


My System SpecsSystem Spec
Old 09-13-2006   #2 (permalink)
Sung M Kim
Guest


 

Re: Sorting a hashtable

There should be other ways to accomplish sorting Hashtable data by
"value"s, but the easiest way is to access the "Values" property of
pipeline object of type Hashtable.

dir | ? {$_.PsIsContainer} | % { $t=0; ls -r $_.Name | % {$t +=
$_.Length }; @{ $_.Name=$t } } | sort {$_.Values}

In the last section of the pipeline, "sort-object"'s input object takes
in script block argument and sort by the "Values" property of the
hashtable object.

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing hashtable values Cookiecutter PowerShell 4 05-20-2008 10:36 AM
How to sort a hashtable? Tibor Soos PowerShell 3 01-31-2008 09:26 AM
Creating a hashtable in the pipeline? gurbao PowerShell 2 05-18-2007 08:03 AM
Filter a hashtable? gurbao PowerShell 1 05-18-2007 01:18 AM
sorting on hashtable values Neil Chambers PowerShell 3 04-08-2007 03:49 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51