How to Sort an Array in PowerShell?

Do you need to know about PowerShell sort arrays? In this PowerShell tutorial, I will explain how to sort an array in PowerShell using various methods and real examples.

To sort an array in PowerShell, use the Sort-Object cmdlet. For ascending order, simply pipe the array into the cmdlet, like $myArray | Sort-Object. To sort in descending order, append the -Descending parameter: $myArray | Sort-Object -Descending.

Sort an Array in PowerShell

An array is a data structure that holds a collection of items. These items can be of any data type, and in PowerShell, arrays can even hold items of different types. Arrays are incredibly useful for storing and manipulating sets of data.

Let us explore different methods to sort a PowerShell array.

Sorting a PowerShell Array with Sort-Object

The primary cmdlet for sorting in PowerShell is Sort-Object. This cmdlet sorts objects in ascending or descending order based on object property values. If you don’t specify a property, PowerShell will sort based on the default property for the object type.

Basic Sorting

Here’s a simple example of sorting an array of numbers in PowerShell using sort-object:

$array = 3, 1, 4, 1, 5, 9, 2
$sortedArray = $array | Sort-Object
$sortedArray

This will output the numbers in ascending order:

1
1
2
3
4
5
9

Here is another example of how to sort a string array in PowerShell.

# Define a string array
$stringArray = 'orange', 'apple', 'banana'

# Sort the array in ascending order using Sort-Object
$sortedArray = $stringArray | Sort-Object

# Output the sorted array
$sortedArray

This script will output the strings ‘apple’, ‘banana’, and ‘orange’ in ascending alphabetical order. You can see the output in the screenshot below after I executed the PowerShell script using VS code:

Sort an Array in PowerShell

If you wanted to sort in descending order, you would modify the Sort-Object line to include the -Descending parameter:

# Sort the array in descending order
$sortedArray = $stringArray | Sort-Object -Descending

You can see the output in the screenshot below; I have used VS code; you can use any editor to execute the PowerShell script.

PowerShell sort array

Sorting in Descending Order

To sort a PowerShell array in descending order, use the -Descending switch:

$sortedArray = $array | Sort-Object -Descending
$sortedArray

Sort String Array in PowerShell

You can also sort a string array in PowerShell. By default, the sort is case-insensitive:

$stringArray = 'Banana', 'apple', 'Cherry'
$sortedStringArray = $stringArray | Sort-Object
$sortedStringArray

This sorts the strings in alphabetical order:

apple
Banana
Cherry

Sort an Array in PowerShell using Custom Expression

Let us see how to sort an array in PowerShell using a custom expression. You can sort based on expressions, such as parts of a string or mathematical calculations:

$array = 'a1', 'a10', 'a2', 'a3'
$sortedArray = $array | Sort-Object { [int]($_.Substring(1)) }
$sortedArray

This will sort the array based on the numerical part of each string:

a1
a2
a3
a10

Sort a PowerShell Array with Multiple Properties

You can also sort a PowerShell array by multiple properties. This is useful when you have complex objects, and you want to sort by one property, then another:

$people = @(
    @{ Name = 'John'; Age = 30 },
    @{ Name = 'Jane'; Age = 25 },
    @{ Name = 'John'; Age = 22 }
)

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

The above PowerShell script will sort people by name and then by age within each name.

Sort Array with Hash Tables in PowerShell

Let me show you how to sort an array with hash tables in PowerShell.You can use hash tables to specify different sorts for different properties:

$sortedPeople = $people | Sort-Object -Property @{Expression="Name";Descending=$false}, @{Expression="Age";Descending=$true}
$sortedPeople

This sorts by name in ascending order and then by age in descending order.

Conclusion

Sorting arrays in PowerShell is a very common requirement, and we can achieve it using various methods. The Sort-Object cmdlet is the go-to for most sorting tasks, with options for sorting in ascending or descending order, case sensitivity, and unique value sorting.

In this PowerShell tutorial, I have explained how to work with PowerShell array sorting, especially different methods to sort a PowerShell array.

Yu may also like: