PowerShell: How to Sort by Specific Column


Often you may want to import data files into PowerShell and then sort the rows based on a specific column.

You can use the following methods to do so:

Method 1: Sort by String Column

Import-Csv $my_file | Sort-Object team

This particular example imports the file specified at the path in the $my_file object, then sorts the rows in ascending order (A to Z) based on the values in the team column.

Note: By default, the Sort-Object cmdlet sorts values in ascending order. To instead sort by values in descending order, simply add the -descending operator at the end of the line.

Method 2: Sort by Numeric Column

Import-Csv $my_file | Sort-Object { [double]$_.points }

This particular example imports the file specified at the path in the $my_file object, then sorts the rows in ascending order (smallest to largest) based on the values in the points column.

Note that we used [double] to cast the points column to a double numeric data type before sorting.

The following examples show how to use each method in practice.

Example 1: How to Sort by String Column in PowerShell

Suppose we have a CSV file saved at the following location:

  • c:\users\bobbi\nba_data.csv

Suppose we use the Import-Csv cmdlet to view the contents of this entire file:

The file contains three columns that show the team, points and assists for various basketball players.

Suppose that we would like to sort the rows by the strings in the team column.

We can use the following syntax to do so:

Import-Csv $my_file | Sort-Object team

The following screenshot shows how to use this syntax in practice:

PowerShell sort by string column in ascending order

Notice that the rows are now sorted in order from A to Z based on the strings in the team column.

If you would instead like to sort from Z to A, simply add the -descending operator at the end of the line:

Import-Csv $my_file | Sort-Object team -descending

The following screenshot shows how to use this syntax in practice:

PowerShell sort by string column in descending order

Notice that the rows are now sorted in order from Z to A based on the strings in the team column.

Example 2: How to Sort by Numeric Column in PowerShell

Once again suppose we have a CSV file saved at the following location:

  • c:\users\bobbi\nba_data.csv

Suppose we use the Import-Csv cmdlet to view the contents of this entire file:

Suppose that we would like to sort the rows by the numeric values in the points column.

We can use the following syntax to do so:

Import-Csv $my_file | Sort-Object { [double]$_.points }

The following screenshot shows how to use this syntax in practice:

PowerShell sort by numeric column in ascending order

Notice that the rows are sorted in ascending order (smallest to largest) based on the value in the points column.

To instead sort the rows by the values in the points column in descending order (largest to smallest), simply add the -descending operator:

Import-Csv $my_file | Sort-Object { [double]$_.points } -descending

The following screenshot shows how to use this syntax in practice:

PowerShell sort by numeric column in descending order

Notice that the rows are now sorted in descending order (largest to smallest) based on the value in the points column.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Group-Object with Multiple Properties
PowerShell: How to Find Duplicate Values in Array
PowerShell: How to Compare Two Arrays

Leave a Reply