Sort data using a custom list in PowerShell


Sometimes you might come across a situation where you’d like to sort a collection based on a custom list (similar to the feature available in Excel), rather than doing a basic sort based on the alphanumeric order of the property values using the Sort-Object cmdlet:

$numberWords | sort

The above doesn’t really do the trick, if one would like to sort the collection based on the semantic order. For one dimensional arrays like the one used in the example above the System.Array type has an overload of the static sort method that can do this (kind of):


While this produces the correct order (changing the order of the items in-place), it does not really scale to a longer list, since it requires the list of ranks to be provided for each element of the array. Furthermore this wouldn’t work for collections of objects. The problem can be better approached by utilizing calculated properties for the Property parameter of the Sort-Object (see …

Get-Help sort -Parameter Property
Get-Help sort -Examples 

for more details):

$numberWords | sort { $customList.IndexOf($_) }

In the example above a scriptblock is used instead of a property Name for the Property parameter. Inside the scriptblock the ranks of the items within the collection ($numberWords) are determined by their position within the custom list ($customList) through the usage of the IndexOf() method. The same approach can also be used to sort collections of objects. Let’s try to sort processes by a custom list of process names:

$customList = 'iexplore', 'excel', 'notepad' 
Get-Process | sort {
	if($rank -ne -1){$rank}

In addition to the approach used in the previous example, here we are also dealing with property values that do not appear in the list. In this case the IndexOf() method returns -1, which would lead the object(s) to show at the top of the list, instead we assign a very high number for those values this makes the object(s) showing up at the bottom of the list. Adding the Name property as an additional Property parameter value produces the additional property values to be sorted alphabetically.
Let’s wrap this into a function for better re-usability:

function Sort-CustomList{
        if($rank -ne -1){$rank}
    if ($additionalProperty){
    $Input | sort $properties
$customList = 'iexplore', 'excel', 'notepad' 
Get-Process | Sort-CustomList $customList Name Name

Sort-CustomList can also be downloaded from my GitHub repository.


Photo Credit: …-Wink-… via Compfight cc

One thought on “Sort data using a custom list in PowerShell

  1. $numberWords | sort { $customList.IndexOf($_) }

    I knew this existed, but it was surprisingly hard to dig up searching. Glad I found this post. FYI if anyone else was needing this as one of several sort properties I verified this does work:
    $prop1 = @{Expression={$customList.IndexOf($_)}}
    $prop2 = @{Expression={[int]$_.”Count”}; Descending=$true }
    $array | sort $prop1, $prop2


I'd love to hear what you think

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s