How To Sort Array Alphabetically In PowerShell?

Recently, I got a requirement to sort an array alphabetically in PowerShell. PowerShell provides different methods for Sorting an array alphabetically. One of the simple and powerful cmdlet called Sort-Object for sorting a PowerShell array. In this blog post, we will explore how to use this cmdlet to sort an array alphabetically in PowerShell with complete examples.

To sort an array alphabetically in PowerShell, you can use the Sort-Object cmdlet. For example, if you have an array $array = 'Houston', 'Chicago', 'New York', you would sort it by piping the array into the cmdlet like this: $array | Sort-Object. This will return the array elements sorted in ascending alphabetical order.

Sort Array Alphabetically In PowerShell

Now, let us see different ways to sort an array alphabetically in PowerShell.

1. Using Sort-Object Cmdlet

The Sort-Object cmdlet is a versatile command in PowerShell that allows you to sort data in an array or a list based on object properties. By default, when you sort strings, it sorts them in ascending alphabetical order. Here’s a basic example:

# Create an array of strings
$names = 'Smith', 'Anderson', 'Wright', 'Mitchell'

# Sort the array alphabetically
$sortedNames = $names | Sort-Object

# Display the sorted array
$sortedNames

In this example, the output will display the names in alphabetical order: Anderson, Mitchell, Smith, Wright.

I have executed the full PowerShell script using VS code, and you can see the output in the screenshot below:

Sort Array Alphabetically In PowerShell

2. Sort a PowerShell Array in Descending Order

If you want to sort the array in descending order in PowerShell, you can use the -Descending switch parameter:

Here is a complete PowerShell script:

# Create an array of strings
$names = 'Smith', 'Anderson', 'Wright', 'Mitchell'

# Sort the array in descending alphabetical order
$sortedNamesDesc = $names | Sort-Object -Descending

# Display the sorted array
$sortedNamesDesc

You can see the output in the screenshot below:

PowerShell Sort Array Alphabetically

3. Sort PowerShell Array Based on String Length

Another common sorting requirement might be to sort the strings based on their length in PowerShell. You can achieve this by using a script block with the Sort-Object cmdlet:

Here is the script to sort a string array alphabetically using string length in PowerShell.

# Create an array of strings
$names = 'Smith', 'Anderson', 'Wright', 'Mitchell'

# Sort the array based on string length
$sortedByLength = $names | Sort-Object { $_.Length }

# Display the sorted array
$sortedByLength

Here is the output you can see in the screenshot below:

powershell sort string array alphabetically

4. Case-Insensitive Sorting

By default, Sort-Object is case-insensitive when sorting strings in PowerShell. However, if you need to perform a case-sensitive sort, you can use the -CaseSensitive switch in PowerShell array.

# Create an array of strings
$names = 'Smith', 'Anderson', 'Wright', 'Mitchell'

# Case-sensitive alphabetical sort
$caseSensitiveSort = $names | Sort-Object -CaseSensitive

# Display the sorted array
$caseSensitiveSort

5. Sorting with Custom Comparisons

For more complex sorting, you can use the [Array]::Sort method in PowerShell with a custom IComparer:

# Custom comparer for reverse alphabetical sorting
$comparer = [System.Collections.Generic.Comparer[string]]::Create({
    param($x, $y)
    return [string]::CompareOrdinal($y, $x)
})

# Convert the array to a list
$list = [System.Collections.Generic.List[string]]$names

# Use the Sort method with the custom comparer
$list.Sort($comparer)

# Display the sorted list
$list

Conclusion

Sorting arrays alphabetically in PowerShell is straightforward with the Sort-Object cmdlet. Whether you’re sorting simple strings, need a descending sort, are sorting by string length, or require a case-sensitive sort, Sort-Object has you covered. For more advanced sorting, you can use the [Array]::Sort method with a custom comparer.

In this PowerShell tutorial, I have explained how to sort array alphabetically In PowerShell using different methods with examples.

You may also like: