Sort-Object

Sort objects by property value.

Syntax
      Sort-Object [[-property] Object[]] [-inputObject psobject] [-culture string]
            [-caseSensitive] [-unique] [-descending] [CommonParameters]

Key
   -Property Object
       A property or properties to use when sorting. Wildcards are permitted. 
       Objects are sorted based on the values of these properties.

       Multiple properties may be specified, the objects will be sorted by 
       each property in turn. i.e Surname and then FirstName

       If not property is specified, sort-object will sort using the default
       property for the object type.

   -CaseSensitive
       Sort UPPER and lower case letters separately.

   -Culture string
       The cultural/country configuration to use when sorting.

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

   -InputObject psobject
       The objects to be sorted.

       When the -InputObject parameter is used 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 multiple items, pipe them to Sort-Object.

       This parameter is an implementation detail: its purpose is to enable input via the pipeline, and its
       direct use with arrays (collections) does not (yet) provide any useful functionality.

   -Unique 
       Return only the unique values.

Standard Aliases for Set-Alias: sort

The -Descending parameter applies to all properties. To sort some properties in ascending order and others in decending order, specify the property values with a hashtable like this:
-property @{expression={$_.PropName};Descending=$true}

Examples

List the files in the current directory and sort using the default order (alphabetical by Name):

PS C:\> get-childitem | sort-object

List the files in the current directory and sort by date/time:

PS C:\> get-childitem | sort -property LastWriteTime

List the files in the current directory and sort by file length (size):

PS C:\> get-childitem | sort -property length

Display the services on the computer, sorted by Status, then by Service DisplayName:

PS C:\> Get-Service | Sort-Object -Property Status, DisplayName

Display the services on the computer in descending Status order and ascending DisplayName order:

PS C:\> get-service | sort-object -property `
@{Expression="Status";Descending=$true}, `
@{Expression="DisplayName";Descending=$false}

List the files in the current directory and sort in descending order by the time span between CreationTime and LastWriteTime:

PS C:\> get-childitem *.* | sort @{Expression={$_.LastWriteTime-$_.CreationTime}; Ascending=$false} | select-object LastWriteTime, CreationTime

Sort a string of characters

$mystring = "12ÇdfDd²4a5617F89aªábCåÅæÆbDecBcCçeEéàâAäÄaÉ33èEêëAdfF"
$mySortedString = $mystring -split '(.{1})' | Where-object{$_} | sort -casesensitive -unique
"$mySortedString"

"We never sit anything out. We are cups, constantly and quietly being filled. The trick is, knowing how to tip ourselves over and let the Beautiful Stuff out" ~ Ray Bradbury

Related PowerShell Cmdlets

Get-Unique - Get the unique items in a collection.
Compare-Object - Compare the properties of objects.
ForEach-Object - Loop for each object in the pipeline.
Group-Object - Group the objects that contain the same value for a common property.
Select-Object - Select properties of objects.
Tee-Object - Send input objects to two places.
Where-Object -
Filter the objects passed along the command pipeline.
Equivalent bash command: sort - Sort text files.


 
Copyright © 1999-2024 SS64.com
Some rights reserved