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.

   -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 objects, pipe them to Sort-Object.

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

   -CaseSensitive
       Sort UPPER and lower case letters separately.

   -Unique 
       Return only the unique values.

   -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.

   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

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

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

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}

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:

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 SS64.com 1999-2013
Some rights reserved