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 here:

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 }
)
$examResults | Sort-Object {
switch ($_.Result) {
'Pass' { 1 }
'Fail' { 2 }
'N/A' { 3 }
}
}

Exam Result Mark
---- ------ ----
Biology Pass 78
Physics Pass 86
Maths Pass 92
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.

As Sort-Object is capable of sorting on more than one property, the preceding example can be taken further to sort on Mark next:

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

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

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

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

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

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 property, based on Result, 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.147.47.166