Private/Sort-ObjectWithCustomList.ps1
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 |
# Thanks to https://gallery.technet.microsoft.com/scriptcenter/Sort-With-Custom-List-07b1d93a Function Sort-ObjectWithCustomList { Param ( [parameter(ValueFromPipeline=$true)] [PSObject] $InputObject, [parameter(Position=1)] [String] $Property, [parameter()] [Object[]] $CustomList ) Begin { # convert customList (array) to hash $hash = @{} $rank = 0 $customList | Select -Unique | ForEach-Object { $key = $_ $hash.Add($key, $rank) $rank++ } # create script block for sorting # items not in custom list will be last in sort order $sortOrder = { $key = if ($Property) { $_.$Property } else { $_ } $rank = $hash[$key] if ($rank -ne $null) { $rank } else { [System.Double]::PositiveInfinity } } # create a place to collect objects from pipeline # (I don't know how to match behavior of Sort's InputObject parameter) $objects = @() } Process { $objects += $InputObject } End { $objects | Sort-Object -Property $sortOrder } } |