Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - sorting on hashtable values

Reply
 
Old 04-07-2007   #1 (permalink)
Neil Chambers


 
 

sorting on hashtable values

Powershellers,

I have a hashtable '$Store' consisting of keys paired to multiple arrays

Name Value
---- ----
Buns { 3 2, 0 0, 0 0, 2 1}
Loaves {4 2, 5 2, 0 0, 0 0}

and the list continues.

what I'd like to do is sort the keys based on the sum of position zero
within each of the arrays

For example, I sum all vaules in position zero thus:

@($i=0; $Store.Keys | %{$Store.$_[0..4]} | %{$i+=$_[0]}; $i)

I can sort the values themselves thus

$store.keys | %{$store.$_[0][0]} | sort

but I need to display the key as well as the vaule and that position must be
based on the cumulative sum of position zero for each of the 4 arrays per
item....

any ideas?

cheers
n


My System SpecsSystem Spec
Old 04-07-2007   #2 (permalink)
Ryan Milligan


 
 

Re: sorting on hashtable values

Instead of evaluating the values and sending the results to sort, just pass
sort a script block, like so:

$Store.Keys | sort { ($Store[$_][0] | measure-object -sum).Sum }

Now what's coming out at the end is a stream of keys instead of values, so
you can access back into the hashtable and grab the values back out and
display them however you like. Hope this helps.

-- Ryan Milligan

"Neil Chambers" <nospamplease@nospam.com> wrote in message
news:ECA2DA43-B943-438A-A06F-029534BE7E62@microsoft.com...
> Powershellers,
>
> I have a hashtable '$Store' consisting of keys paired to multiple arrays
>
> Name Value
> ---- ----
> Buns { 3 2, 0 0, 0 0, 2 1}
> Loaves {4 2, 5 2, 0 0, 0 0}
>
> and the list continues.
>
> what I'd like to do is sort the keys based on the sum of position zero
> within each of the arrays
>
> For example, I sum all vaules in position zero thus:
>
> @($i=0; $Store.Keys | %{$Store.$_[0..4]} | %{$i+=$_[0]}; $i)
>
> I can sort the values themselves thus
>
> $store.keys | %{$store.$_[0][0]} | sort
>
> but I need to display the key as well as the vaule and that position must
> be based on the cumulative sum of position zero for each of the 4 arrays
> per item....
>
> any ideas?
>
> cheers
> n



My System SpecsSystem Spec
Old 04-07-2007   #3 (permalink)
Duncan Smith


 
 

Re: sorting on hashtable values

On Apr 7, 9:16 pm, "Neil Chambers" <nospample...@nospam.com> wrote:
> Powershellers,
>
> I have a hashtable '$Store' consisting of keys paired to multiple arrays
>
> Name Value
> ---- ----
> Buns { 3 2, 0 0, 0 0, 2 1}
> Loaves {4 2, 5 2, 0 0, 0 0}
>
> and the list continues.
>
> what I'd like to do is sort the keys based on the sum of position zero
> within each of the arrays
>
> For example, I sum all vaules in position zero thus:
>
> @($i=0; $Store.Keys | %{$Store.$_[0..4]} | %{$i+=$_[0]}; $i)
>
> I can sort the values themselves thus
>
> $store.keys | %{$store.$_[0][0]} | sort
>
> but I need to display the key as well as the vaule and that position must be
> based on the cumulative sum of position zero for each of the 4 arrays per
> item....
>
> any ideas?
>
> cheers
> n


Here's a nuts and bolts approach, though Ryan's already come up with a
more elegant solution! I was thinking along the lines of creating a
second hashtable to store the order, like so:

# Define two two-dimensional arrays
$bunsArray = New-Object 'object[,]' 2,4
$loavesArray = New-Object 'object[,]' 2,4

# Populate the arrays
$bunsArray[0,0]=3
$bunsArray[1,0]=2
$bunsArray[0,1]=0
$bunsArray[1,1]=0
$bunsArray[0,2]=0
$bunsArray[1,2]=0
$bunsArray[0,3]=2
$bunsArray[1,3]=1

$loavesArray[0,0]=4
$loavesArray[1,0]=2
$loavesArray[0,1]=5
$loavesArray[1,1]=2
$loavesArray[0,2]=0
$loavesArray[1,2]=0
$loavesArray[0,3]=0
$loavesArray[1,3]=0

# Create a hashtable and populate it with the two arrays
$store = @{}
$store.add('Buns', $bunsArray)
$store.add('Loaves', $loavesArray)

# Create a hashtable keyed on the sum of the first elements of each
array, mapped onto the store key
$order=@{}

#Iterate through the arrays in the store, sum up the first elements,
and populate the order hashtable
$store.keys |% {
$total=$dim0=$dim1=0;

while($dim1 -lt $store.$_.GetLength(1)) {
$total += $store.$_[($dim0,$dim1)]; $dim1++
}

$order.add($total,$_)
}

# display each item from the store along with its first element count,
followed by all element values, all
# ordered on the first element count (ascending)
$order.keys | sort |% {
[string]$order.$_ + ", count: $_, value: " + [string]$store.
($order.$_)[(0,0),(1,0),(0,1),(1,1),(0,2),(1,2),(0,3),(1,3)]
}

Regards,

Duncan.

My System SpecsSystem Spec
Old 04-08-2007   #4 (permalink)
Neil Chambers


 
 

Re: sorting on hashtable values

aha! hello 'Measure-Object'. Where have you been all my life ;-)

Thanks to Ryan, for introducing me to this lovely cmdlet. And Thank you
Duncan, for showing me behind the curtain!

: n)

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
comparing hashtable values PowerShell
Export-Csv multiple hashtable values PowerShell
How to sort a hashtable? PowerShell
Assigning new values to a hashtable PowerShell
Sorting a hashtable PowerShell


Vista Forums 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 Ltd

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