PowerShell Custom Order Sorting PowerShell Custom Order Sorting powershell powershell

PowerShell Custom Order Sorting


You can use Array.IndexOf(), which effectively translates strings into numbers:

$importance = "Failed", "Warning", "Success"$result | Sort-Object { $importance.IndexOf($_.Result) }

Catch: Any unexpected value in Result will be sorted to the top, because IndexOf will return -1 for values it can't find.

Test:

$importance = "Failed", "Warning", "Success"$list = @(    @{ Result = "Warning" }    @{ Result = "Success" }    @{ Result = "Failed" })$list | Sort-Object { $importance.IndexOf($_.Result) }

Result:

Name                           Value                                                                                                                                                                 ----                           -----                                                                                                                                                                 Result                         Failed                                                                                                                                                                Result                         Warning                                                                                                                                                               Result                         Success                                                                                                                                                               


Here is a script block option:

ForEach ($Result in 'Success','Warning','Failed') {    $Obj | Where-Object {$_.Result -eq $Result } | Sort-Object Name}

Assumes that your cmdlet has output the original object into a variable named $Obj.


Here is how I did this, you pass the encompassing object and the property name you want to sort on and it brings you back a properly sorted object without having to try and work around the sort-object command (which is awesome) without knowing what's going on under its hood. Hopefully this helps.

function sortObjectManually{    Param(    [parameter(Mandatory=$true)]    $unsortedObject,    [parameter(Mandatory=$true)]    $propertyName    )        $sortedObject = New-Object System.Collections.ArrayList;    foreach ($object in $unsortedObject)    {        if ($sortedObject.Count -eq 0)        {            $sortedObject.Add($object) | Out-Null;        }        else        {            $inserted = $false;            for ($i = 0; $i -lt $sortedObject.Count; $i++)            {                if ($object.$propertyName -lt $sortedObject[$i].$propertyName)                {                    $sortedObject.Insert($i, $object);                    $inserted = $true;                    break;                }            }                        if ($inserted -eq $false)            {                $sortedObject.Add($object) | Out-Null;            }        }    }        return $sortedObject;}cls;$prop = "ServerName";$results = sortObjectManually -unsortedObject $endInvokeArr -propertyName $prop;