Sort Hashtable with Arrays as values

Viewed 6127

Description: I'm building a PowerShell-script that searches for files, then gives them unique names, copies them and then verifies them via hash-calculation - I chose to split the script in functions for each step, so it's easier to maintain the whole thing. To get all values from one function to the other, I chose to use [hashtable]$FooBar - inside $FooBar, there are multiple arrays, such as FullName or OutputPath (which may change per file as they will be copied to subfolders named yyyy-mm-dd). All arrays are correlating with each other (meaning that index 1 contains all values of the first file, index 2 the values for the second file,...) and this works fine as of now.

A short simplified visualisation:

$FooBar = @{}
$FooBar.FullName = @()
$FooBar.Size = @()
$FooBar.Ext = @()
Get-ChildItem | ForEach-Object {
    $FooBar.FullName += $_.FullName
    $FooBar.Size += $_.Length
    $FooBar.Ext += $_.Extension
}

However, I now need to sort them all by one value-set of one of the arrays, e.g. the size. Or, visualised again:

# From:
$FooBar
Name                           Value
----                           -----
fullname                       {D:\AAA.XYZ, D:\BBB.ZYX, D:\CCC.YZX}
size                           {222, 111, 555}
extension                      {.XYZ, .ZYX, .YZX}

# To:
$FooBar = $FooBar | Sort-Object -Property Size -Descending
$FooBar
Name                           Value
----                           -----
fullname                       {D:\CCC.YZX, D:\AAA.XYZ, D:\BBB.ZYX}
size                           {555, 222, 111}
extension                      {.YZX, .XYZ, .ZYX}

I tried $FooBar.GetEnumerator() | Sort-Object -Property Size, but this does not change anything. Google turned up suggestions on how to sort an array of hashtables, but in my case, it's the other way round, and I can't get my head around this because I don't even understand why this is a problem in the first place.

So my question is: is there any way to sort all arrays inside the hashtable by the value-set of one of the arrays? I can't get my head around this.

Disclaimer: I'm a PowerShell-autodidact with no reasonable background in scripting/programming, so it might well be that my "include everything in one hashtable"-solution isn't going to work at all or might be extremely inefficient - if so, please tell me.

3 Answers
Related