Sort-Object

Updated: August 9, 2015

Sort-Object

Sorts objects by property values.

Aliases

The following abbreviations are aliases for this cmdlet: 

  • sort

Syntax

Parameter Set: Default
Sort-Object [[-Property] <Object[]> ] [-CaseSensitive] [-Culture <String> ] [-Descending] [-InputObject <PSObject> ] [-Unique] [ <CommonParameters>]




Detailed Description

The Sort-Object cmdlet sorts objects in ascending or descending order based on the values of properties of the object.

You can specify a single property or multiple properties, for a multi-key sort, and you can select a case-sensitive or case-insensitive sort. You can also direct Sort-Object to display only the objects with a unique value for a particular property.

Parameters

-CaseSensitive

Indicates that the sort should be case sensitive. By default, sorting is not case sensitive.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Culture<String>

Specifies the cultural configuration to use when sorting.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Descending

Indicates that the cmdlet sorts the objects in descending order. The default is ascending order.

The Descending parameter applies to all properties. To sort by some properties in ascending order and others in descending order, you must specify their property values by using a hash table.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-InputObject<PSObject>

Specifies the objects to sort.

When you use the InputObject parameter to submit a collection of items, Sort-Object receives one object that represents the collection. Because one object cannot be sorted, Sort-Object returns the entire collection unchanged.

To sort objects, pipe them to Sort-Object.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByValue)

Accept Wildcard Characters?

false

-Property<Object[]>

Specifies the properties to use when sorting. Objects are sorted based on the values of these properties. Enter the names of the properties. Wildcards are permitted.

If you specify multiple properties, the objects are first sorted by the first property. If more than one object has the same value for the first property, those objects are sorted by the second property. This process continues until there are no more specified properties or no groups of objects.

If you do not specify properties, the cmdlet sorts based on default properties for the object type.

The value of the Property parameter can be a calculated property. To create a calculated, property, use a hash table. Valid keys are:

-- Expression <string> or <script block>
-- Ascending <Boolean>
-- Descending <Boolean>


Aliases

none

Required?

false

Position?

1

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Unique

Indicates that the cmdlet eliminates duplicates and returns only the unique members of the collection. You can use this parameter instead of using the Get-Unique cmdlet.

This parameter is case-insensitive. As a result, strings that differ only in character casing are considered to be the same.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see    about_CommonParameters.

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • System.Management.Automation.PSObject

    You can pipe the objects to be sorted to Sort-Object.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • System.Management.Automation.PSObject

    Sort-Object returns the sorted objects.


Notes

  • Sort-Object sorts objects based on the properties that you specify or the default sort properties for objects of that type.

  • If an object does not have one of the specified properties, the property value for that object is interpreted by the cmdlet as Null and is placed at the end of the sort order.

  • When sorting objects, Sort-Object uses the Compare method for each property. If a property does not implement IComparable, the cmdlet converts the property value to a string and uses the Compare method for System.String.

  • The Sort-Object cmdlet sorts objects in ascending or descending order based on the values of properties of the object.

  • If you sort on a property whose value is an enumeration, Sort-Object sorts the enumeration values in numeric order; it does not sort the enumeration member names. For example, if you sort services by status, services with a status of Stopped appear before services with a status of Running, because the value of Status is a ServiceControllerStatus enumeration, in which Stopped has a value of 1 and Running has a value of 4.

Examples

Example 1: Sort the current directory by name

This command sorts the subdirectories and files in the current directory. Because no properties are specified, the files and directories are sorted in ascending alphabetical order by their default sort property, Name.


PS C:\> Get-ChildItem | Sort-Object

Example 2: Sort the current directory by file length

This command displays the files in the current directory in ascending order by file length.


PS C:\> Get-ChildItem | Sort-Object -Property length

Example 3: Sort processes by memory use

This command displays the five processes on the computer with the greatest memory use based on the size of their working sets.

The command uses the Get-Process cmdlet to get a list of processes. It uses a pipeline operator (|) to send the results to the Sort-Object cmdlet, which sorts the objects in working-set order.

Another pipeline operator sends the results to the Select-Object, which displays only the last five items in the list.


PS C:\> Get-Process | Sort-Object -Property WS | Select-Object -Last 5

Example 4: Sort HistoryInfo objects by ID

This command sorts HistoryInfo objects using the Id property as the default key.


PS C:\> Get-History | Sort-Object -Descending

Example 5: Sort services by Status and DisplayName

This command displays the services on the computer in descending Status order and ascending DisplayName order.

The command uses the Get-Service cmdlet to get the services on the computer. It uses a pipeline operator (|) to send services to the Sort-Object cmdlet.

To sort one property in ascending order and another property in descending order, the command uses a hash table for the value of the Property parameter. The hash table uses an Expression key to specify the property name and an Ascending or Descending key to specify the sort order.

The resulting display, which sorts the Status values in descending order, lists properties with a Status value of Running before those with a Status value of Stopped. When sorted in ascending order, Stopped appears before Running, because Status is an enumerated property in which the value of Stopped (1) is less than the value of Running (4).


PS C:\> Get-Service | Sort-Object -Property @{Expression = "Status"; Descending = $True}, @{Expression = "DisplayName"; Descending = $False}

Example 6: Sort text files by time span

This command sorts text files in descending order by the time span between CreationTime and LastWriteTime.


PS C:\> Get-ChildItem *.txt | Sort-Object -Property @{Expression={$_.LastWriteTime - $_.CreationTime}; Ascending = $False} | Format-Table LastWriteTime, CreationTime

Example 7: Sort names in a text file

These commands sort the names of servers in a text file. The second command uses the Sort-Object cmdlet with the Unique parameter to return a sorted list without duplicates.


PS C:\> Get-Content servers.txt
PS C:\> Get-Content servers.txt | Sort-Object -Unique

Related topics

Community Additions

ADD
Show: