How to Sort an Array of Objects in PowerShell?

To sort an array of objects, PowerShell provides Sort-Object cmdlet. In this PowerShell tutorial, we’ll explore how to use this cmdlet to sort arrays of objects in PowerShell with real examples.

To sort an array of objects in PowerShell, use the Sort-Object cmdlet specifying the property names to sort on. For instance, $arrayOfObjects | Sort-Object -Property PropertyName sorts the array based on ‘PropertyName’. You can sort on multiple properties and in both ascending and descending order.

What Are Objects and Arrays in PowerShell

Let us understand objects and arrays in PowerShell first. In PowerShell, everything is an object. An object is a data structure that stores data (properties) and actions (methods). An array is a collection of objects that can be of the same type or different types.

PowerShell Sort-Object Cmdlet

The Sort-Object cmdlet is the primary tool for sorting in PowerShell. It sorts objects in ascending or descending order based on one or more of their property values.

Basic Sorting

The simplest form of sorting with Sort-Object is by a single property in PowerShell. Here’s an example:

$people = @(
    [PSCustomObject]@{Name='Alice'; Age=25},
    [PSCustomObject]@{Name='Bob'; Age=30},
    [PSCustomObject]@{Name='Charlie'; Age=22}
)

$sortedPeople = $people | Sort-Object -Property Age
$sortedPeople

The above script creates an array of custom objects, each representing a person with a name and age. Using Sort-Object, we sort the array by the Age property. By default, the sorting is in ascending order.

You can see the output in the screenshot below:

Sort an Array of Objects in PowerShell

Sorting in Descending Order

To sort an array of objects in descending order in PowerShell, use the -Descending switch:

$sortedPeopleDescending = $people | Sort-Object -Property Age -Descending
$sortedPeopleDescending

Sorting by Multiple Properties

You can also sort by multiple properties by passing a comma-separated list to the -Property parameter.

Here is an example of a sort array of objects in PowerShell by multiple parameters.

$sortedPeopleMultiple = $people | Sort-Object -Property Age, Name
$sortedPeopleMultiple

This will sort the people by age first and then by name within each age group.

Custom Sorting with Script Blocks

For more complex sorting rules, you can use script blocks. A script block allows you to specify an expression for sorting:

$sortedPeopleScriptBlock = $people | Sort-Object -Property { $_.Name.Length }
$sortedPeopleScriptBlock

This will sort the people based on the length of their names.

Advanced Sorting Techniques

Case-Insensitive Sorting

PowerShell’s sorting is case-sensitive by default. To perform a case-insensitive sort, you can use a script block and the ToLower() or ToUpper() methods:

$sortedPeopleCaseInsensitive = $people | Sort-Object -Property { $_.Name.ToLower() }
$sortedPeopleCaseInsensitive

Sorting with Custom Objects and Hashtables

Sometimes, you may need to sort by a property that isn’t directly accessible or doesn’t exist. In such cases, you can create a custom property on the fly using a hashtable:

$sortedPeopleCustomProperty = $people | Sort-Object -Property @{Expression={$_.Name.Length}; Ascending=$true}
$sortedPeopleCustomProperty

This sorts the people by the length of their name, similar to the script block example, but it’s more verbose and allows for additional options like specifying the direction of the sort per property.

  • When sorting, always ensure the properties you’re sorting by exist on all objects in the array.
  • Remember that Sort-Object creates a new sorted array; it doesn’t sort the original array in place.
  • Use the Get-Member cmdlet to explore the properties available on your objects, which can help you understand what you can sort by.

Conclusion

Sorting arrays of objects in PowerShell is straightforward with the Sort-Object cmdlet. Whether you’re sorting by a single property, multiple properties, or using complex expressions, Sort-Object provides the flexibility needed for your data organization tasks.

In this PowerShell tutorial, I have explained how to sort an array of objects in PowerShell using the sort-object method.

You may also like: