The Sort-Object command

The Sort-Object command allows objects to be sorted on one or more properties.

By default, Sort-Object will sort numbers in ascending order:

PS> 5, 4, 3, 2, 1 | Sort-Object
1
2
3
4
5

Strings are sorted in ascending order, irrespective of uppercase or lowercase:

PS> 'ccc', 'BBB', 'aaa' | Sort-Object
aaa
BBB
ccc

When dealing with complex objects, Sort-Object may be used to sort based on a named property. For example, processes may be sorted based on the Id property:

Get-Process | Sort-Object -Property Id 

Objects may be sorted on multiple properties; for example, a list of files may be sorted on LastWriteTime and then on Name:

Get-ChildItem C:WindowsSystem32 | 
    Sort-Object LastWriteTime, Name 

In the preceding example, items are first sorted on LastWriteTime. Items that have the same value for LastWriteTime are then sorted based on Name.

Sort-Object is not limited to sorting on existing properties. A script block (a fragment of script, enclosed in curly braces) can be used to create a calculated value for sorting. For example, it is possible to order items based on a word, as shown in this example:

PS> $examResults = @(
>> [PSCustomObject]@{ Exam = 'Music'; Result = 'N/A'; Mark = 0 }
>> [PSCustomObject]@{ Exam = 'History'; Result = 'Fail'; Mark = 23 }
>> [PSCustomObject]@{ Exam = 'Biology'; Result = 'Pass'; Mark = 78 }
>> [PSCustomObject]@{ Exam = 'Physics'; Result = 'Pass'; Mark = 86 }
>> [PSCustomObject]@{ Exam = 'Maths'; Result = 'Pass'; Mark = 92 }
>> )
PS> $examResults | Sort-Object {
>> switch ($_.Result) {
>> 'Pass' { 1 }
>> 'Fail' { 2 }
>> 'N/A' { 3 }
>> }
>> }

Exam Result Mark
---- ------ ----
Maths Pass 92
Physics Pass 86
Biology Pass 78
History Fail 23
Music N/A 0

In the preceding example, when Sort-Object encounters a pass result, it is given the lowest numeric value (1). As Sort-Object defaults to ascending ordering, this means exams with a result of pass appear first in the list. This process is repeated to give a numeric value to each of the other possible results.

Sorting within the set varies depending on the version of PowerShell. Windows PowerShell will change the order of the elements within each set. PowerShell Core on the other hand maintains the original order, listing Biology, then Physics, then Maths within the pass set.

As Sort-Object is capable of sorting on more than one property, the preceding example can be taken further to sort on mark next. This makes the output order entirely predictable, regardless of the version of PowerShell:

PS> $examResults | Sort-Object { 
>>     switch ($_.Result) { 
>>         'Pass' { 1 } 
>>         'Fail' { 2 } 
>>         'N/A'  { 3 } 
>>     } 
>> }, Mark

Exam Result Mark

---- ------ ----
Biology Pass 78
Physics Pass 86
Maths Pass 92
History Fail 23
Music N/A 0

Adding the Descending parameter to Sort-Object will reverse the order of both fields:

PS> $examResults | Sort-Object { 
>>     switch ($_.Result) { 
>>         'Pass' { 1 } 
>>         'Fail' { 2 } 
>>         'N/A'  { 3 } 
>>     } 
>> }, Mark -Descending

Exam Result Mark

---- ------ ----
Music N/A 0
History Fail 23
Maths Pass 92
Physics Pass 86
Biology Pass 78

The ordering behavior can be made property-specific using the notation that's shown in the following example:

PS> $examResults | Sort-Object { 
>>     switch ($_.Result) { 
>>         'Pass' { 1 } 
>>         'Fail' { 2 } 
>>         'N/A'  { 3 } 
>>     } 
>> }, @{ Expression = { $_.Mark }; Descending = $true } 

Exam Result Mark
---- ------ ----
Maths Pass 92
Physics Pass 86
Biology Pass 78
History Fail 23
Music N/A 0

The hashtable, @{}, is used to describe an expression (a calculated property; in this case, the value for mark) and the sorting order, which is either ascending or descending.

In the preceding example, the first sorting property, based on the result property, is sorted in ascending order as this is the default. The second property, mark, is sorted in descending order.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.97.248