Как сортировать массив по алфавиту в power shell


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

Method 1: Sort by String Column

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

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.

Example 1: How to Sort by String Column in PowerShell

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

Как сортировать массив по алфавиту в power shell

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.

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:

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

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

:/>  Почему на ноутбуке не работает клавиатура вся виндовс 10

Как сортировать массив по алфавиту в power shell

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

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:

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.

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

Оставьте комментарий