Private/Sort-CustomList.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 |
# Credit to Dirk Bremen - https://powershellone.wordpress.com/2015/07/30/sort-data-using-a-custom-list-in-powershell/ function Sort-CustomList { # Example: # 'ab', '1', 'cba' | Sort-CustomList -List 1, cba, ab -Verbose # Sort incoming objects according to order in -List [cmdletbinding()] param( [Parameter(ValueFromPipeline=$true)]$InputObject, [object[]]$List, [string]$SortOnProperty, [object[]]$Property ) begin { $In = New-Object System.Collections.ArrayList } process { [void]$In.add($InputObject) } end { $properties = ,{ if($SortOnProperty) {$SortOn = $_."$SortOnProperty"} else {$SortOn = $_} $rank = $List.IndexOf($SortOn) if($rank -ne -1){$rank} else{[System.Double]::PositiveInfinity} } if ($Property){ $properties += $Property } $In | Sort-Object $properties } } |