Skip to main content

Sort an Array or List of Items

Problem

You want to sort the elements of an array or list.

Solution

To sort a list of items, use the SortObject cmdlet: PS >GetChildItem | SortObject Descending Length | Select Name,Length

Name
Length

ConvertTextObject.ps1
6868

ConnectWebService.ps1
4178

SelectFilteredObject.ps1
3252

GetPageUrls.ps1
2878

GetCharacteristics.ps1
2515

GetAnswer.ps1
1890

NewGenericObject.ps1
1490

InvokeCmdScript.ps1
1313

Discussion

The SortObject cmdlet provides a convenient way for you to sort items by a property that you specify. If you don’t specify a property, the SortObject cmdlet follows the sorting rules of those items if they define any.

In addition to sorting by a property in ascending or descending order, the SortObject cmdlet’s –Unique switch also allows you to remove duplicates from the sorted collection.

For more information about the SortObject cmdlet, type GetHelp SortObject.

Help Category