Concept Flow - Sort-Object for ordering
Input Collection
Sort-Object Command
Compare Items
Reorder Items
Output Sorted Collection
Sort-Object takes a list, compares items, reorders them, and outputs the sorted list.
1..5 | Sort-Object -Descending "apple","banana","cherry" | Sort-Object
| Step | Input | Sort Direction | Action | Output |
|---|---|---|---|---|
| 1 | [1,2,3,4,5] | Descending | Compare numbers | [5,4,3,2,1] |
| 2 | ["apple","banana","cherry"] | Ascending | Compare strings alphabetically | ["apple","banana","cherry"] |
| 3 | N/A | N/A | End of sorting | Sorted collections output |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| Numbers | [1,2,3,4,5] | [5,4,3,2,1] | [5,4,3,2,1] | [5,4,3,2,1] |
| Strings | ["apple","banana","cherry"] | ["apple","banana","cherry"] | ["apple","banana","cherry"] | ["apple","banana","cherry"] |
Sort-Object sorts collections in PowerShell. Use -Descending to sort from largest to smallest. Default is ascending order (smallest to largest). Works on numbers, strings, and objects. Outputs a reordered collection.
Sort-Object do?$numbers in descending order?-Descending with a dash and full word.$numbers | Sort-Object -Descending uses the correct dash and spelling. Options A, C, and D have syntax errors or wrong parameter format.$data = @( @{Name='John'; Age=30}, @{Name='Anna'; Age=25}, @{Name='Mike'; Age=35} )
$data | Sort-Object Age | ForEach-Object { $_.Name }$items = @(5, 3, 8, 1)
$items | Sort-Object -descendingg
-Descending, but it is misspelled as -descendingg.$people = @( @{Name='Sam'; Score=90}, @{Name='Liz'; Score=90}, @{Name='Tom'; Score=85} )$people | Sort-Object -Property @{Expression='Score';Descending=$true}, @{Expression='Name';Descending=$false} uses script blocks to specify descending for Score and ascending for Name correctly. $people | Sort-Object -Property Score, Name -Descending applies descending to both, which is wrong. $people | Sort-Object Score -Descending | Sort-Object Name sorts twice, losing the first order. $people | Sort-Object Name, Score -Descending sorts Name first, which is incorrect order.Pass the test and this pattern moves unknown