Sort-Object

 

Additional Resources for Sort-Object

 

Sorting Returned Data

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/sort-object.mspx

 

 

SYNOPSIS

Sorts objects by property values.

 

SYNTAX

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

 

DETAILED DESCRIPTION

Sorts a set of objects by property values. You specify a property or set of properties to sort on by using the Property parameter. If multiple properties are specified, the objects are sorted by the first property. The additional properties are used only if there is at least one group of objects that have the same value for the first property. In that case, the groups are further sorted by the second property. This process continues until there are no more specified properties or no groups of objects.

 

Comparisons made while sorting are case-insensitive unless the CaseSensitive parameter is specified. The cmdlet sorts objects in ascending order, but you can change this default by specifying the Descending parameter. The Descending parameter applies to all of the properties used in the sorting process. To sort by some properties in ascending order and others in decending order, you must specify their property values by using a hashtable like the following:

 

@{expression={$_.PropName},Descending=$true}.

 

Specifying this hashtable as a property value causes the cmdlet to sort on the value of the PropName property in descending order.

 

If an object does not have one of the specified properties, the corresponding property value for that object is interpreted by the cmdlet as NULL. Regardless of the type of property, objects with a NULL value are placed at the end of the sort order.

 

If you do not specify any properties to sort by, the cmdlet will sort by using default methods that depend upon the type of object being sorted.

 

PARAMETERS

 

-property <Object[]>

Specifies a property or properties to use when sorting. The value of this parameter can be a hash table like @{Expression={$_.PropName}; Ascending=$false} or @{Expression={$_.PropName}; Ascending=$true}, letting you sort on the result of a script block and enabling you to specify a different sort order for each element used in the sort.

 

Required?

false

Position?

1

Default value

entire input objects

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-inputObject <psobject>

Specifies the objects to be sorted. You can either use this parameter or pipe an object to the cmdlet.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

true (ByValue)

Accept wildcard characters? 

false

 

-culture <string>

Specifies the cultural configuration to use when sorting.

 

Required?

false

Position?

named

Default value

Current Culture

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-caseSensitive <SwitchParameter>

Indicates that the sort should be case sensitive.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-unique <SwitchParameter>

Sorts on only the unique values of properties.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-descending <SwitchParameter>

Sorts the objects in descending order.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".

 

INPUT TYPE

Object

 

RETURN TYPE

Sorted objects

 

NOTES

 

The order of input objects is not preserved within groups of sorted objects.

 

When making comparisons to sort the input objects, the cmdlet tries to use the Compare method of each property. If a property does not implement IComparable, then the cmdlet converts its value to a string and uses the System.String Compare method.

 

For more information, type "Get-Help Sort-Object  -detailed". For technical information, type "Get-Help Sort-Object -full".

 

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

 

EXAMPLE 1

 

get-childitem | sort-object

 

This command gets the sub-directories and files in the current directory. The sort-object cmdlet, used without parameters, displays results in alphabetical order by name.

 

 

Directory: Microsoft.Management.Automation.Core\FileSystem::C:\Ps

 

Mode  LastWriteTime          Length Name

-----  ------------          ------ ----

-a--- 9/13/2005   4:24 PM    0 0

-a---  9/6/2005   4:19 PM    12     a.csv

-a--- 9/21/2005   3:49 PM    529    a.Ps

-a--- 8/22/2005   4:14 PM    22     a.pl

-a--- 9/27/2005  10:33 AM    24     a.txt

-a--- 9/15/2005  10:31 AM    398    a.vbs

-a--- 7/21/2005  12:39 PM    37066  a.xml

-a--- 8/28/2005  11:30 PM    5412   a.xslt

-a---10/25/2005   1:59 PM    125    AdamTravel.txt

-a--- 7/21/2005   9:49 AM    59     add2Num.Ps

-a--- 8/29/2005   5:42 PM    7111   add-content.xml

-a--- 9/21/2005  12:46 PM    8771   aliens.Ps

-a--- 8/10/2005   2:10 PM    798    array.xml

-a---  8/4/2004   5:00 AM    110    AUTORUN.INF

-a---  9/6/2005   4:20 PM    245    b.csv

 

EXAMPLE 2

 

get-childitem | sort-object -property length

 

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

 

Mode  LastWriteTime          Length    Name

----- ------------           ------    ----

-a---  8/17/2005   9:34 AM   0         gps.txt

-a---  9/13/2005   4:24 PM   0         0

-a---  8/4/2004    5:00 AM   10        WIN51

-a---  8/4/2004    5:00 AM   10        WIN51IP

-a---  8/4/2004    5:00 AM   10        WIN51IP.SP2

-a---  9/6/2005    4:19 PM   12        a.csv

-a--- 12/10/2005   6:04 PM   22        testfile1.txt

-a--- 12/10/2005   6:04 PM   22        test1.txt

...

 

EXAMPLE 3

 

get-process | sort-object -property WS | select-object -last 5

 

This command uses the get-processcmdlet to get a list of processes and pipe it to sort-object which sorts on the working set (WS) property. The result is piped to select-object which shows just the last 5 items in the list. Since sort-object returns a list in ascending order, this gives the processes with the highest memory use. The default display is a table.

 

Handles  NPM(K)PM(K)  WS(K)   VS(M)   CPU(s)    Id    ProcessName

-------  -----------  -----   -----   ------    --    -----------

   2754  19526748     38960   182     129.48   1292   svchost

    773  1925108      41604   136     161.38   2956   explorer

    557  1723864      50600   256      49.63   4388   WINWORD

   1581  6781720      66332   450   1,563.48   3868   INFOPATH

    921  1375112      78928   205      46.84    676   Ps

 

EXAMPLE 4

 

get-history | sort-object -descending

 

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

 

Id CommandLine

-- -----------

51 get-history | sort -descending

50 get-history | sort -descending

49 get-history | sort -descending

48 get-history | sort -descending

47 get-history | sort -descending

46 get-history | sort -descending

45 get-history | sort -descending

44 cd \monad

43 get-childitem | sort-object

42 gci *.txt | Sort-Object @{Expression={$_.LastWriteTime-$_.CreationT...

 

EXAMPLE 5

 

get-childitem *.txt | sort-object @{Expression={$_.LastWriteTime-$_.CreationTime}; Ascending=$false} | select-object LastWriteTime, CreationTime

 

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

 

LastWriteTime            CreationTime

-------------            ------------

2/21/2006 10:22:20 AM    10/3/2005 4:19:40 PM

2/27/2006 8:14:24 AM     2/23/2006 10:41:08 PM

2/24/2006 1:26:19 PM     2/23/2006 11:23:36 PM

1/5/2006 12:01:35 PM     1/5/2006 11:35:30 AM

2/24/2006 9:25:40 AM     2/24/2006 9:22:24 AM

2/24/2006 9:40:01 AM     2/24/2006 9:39:41 AM

2/21/2006 10:21:30 AM    2/21/2006 10:21:30 AM

 

RELATED LINKS

Get-Unique

Compare-Object

ForEach-Object

Group-Object

Measure-Object

New-Object

Select-Object

Tee-Object

Where-Object