Glowie
Glowie

Reputation: 2309

Powershell sort hashtable based on count

I have code that takes array $iis_stats and displays (1) the content NAME (2) number of occurrences for this content Value

$iis_stats | group | % { $h = @{} } { $h[$_.Name] = $_.Count } {$h}

How do I sort $h by value in descending order and print it?

I tried

$h | sort-object @{Expression={$_[1]}; Ascending=$false} {$h}

And I get error:

Sort-Object : A positional parameter cannot be found that accepts argument '$h'.
At D:\Script\parse_IIS_logs.ps1:45 char:6
+ $h | sort-object @{Expression={$_[1]}; Ascending=$false} {$h}
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Sort-Object], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SortObjectCommand

How to fix?

Upvotes: 0

Views: 289

Answers (1)

mjolinor
mjolinor

Reputation: 68273

Like this?

$h.getenumerator() | sort value -descending 

Upvotes: 1

Related Questions