Bird
Raised Fist0
PowerShellscripting~10 mins

Sort-Object for ordering in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available
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.
Execution Sample
PowerShell
1..5 | Sort-Object -Descending
"apple","banana","cherry" | Sort-Object
Sorts numbers descending and sorts strings ascending alphabetically.
Execution Table
StepInputSort DirectionActionOutput
1[1,2,3,4,5]DescendingCompare numbers[5,4,3,2,1]
2["apple","banana","cherry"]AscendingCompare strings alphabetically["apple","banana","cherry"]
3N/AN/AEnd of sortingSorted collections output
💡 All items compared and reordered; sorting complete.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
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"]
Key Moments - 2 Insights
Why does Sort-Object output numbers in descending order when -Descending is used?
Because the -Descending flag tells Sort-Object to reorder items from largest to smallest, as shown in execution_table row 1.
Why do strings sort alphabetically by default?
Sort-Object sorts strings in ascending order by default, comparing letters from A to Z, as seen in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after sorting numbers in descending order?
A[1,2,3,4,5]
B[5,4,3,2,1]
C[3,2,1,5,4]
D[1,3,5,2,4]
💡 Hint
Check execution_table row 1 Output column.
At which step does Sort-Object finish sorting the string list?
AStep 3
BStep 1
CStep 2
DSorting never finishes
💡 Hint
Look at execution_table rows and see when strings are sorted.
If you remove -Descending from the number sort, what would the output be?
A[1,2,3,4,5]
B[3,2,1,5,4]
C[5,4,3,2,1]
D[2,4,1,3,5]
💡 Hint
Default sort order is ascending; see variable_tracker Numbers after Step 1 for descending.
Concept Snapshot
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.
Full Transcript
Sort-Object takes a list of items and sorts them. When you use -Descending, it sorts from biggest to smallest. Without it, sorting is from smallest to biggest. For example, numbers 1 to 5 sorted descending become 5,4,3,2,1. Strings sort alphabetically by default. The command compares items, reorders them, and outputs the sorted list. This process stops when all items are sorted.

Practice

(1/5)
1. What does the PowerShell cmdlet Sort-Object do?
easy
A. It creates new objects from scratch.
B. It arranges objects in a specified order based on property values.
C. It deletes objects from a list.
D. It counts the number of objects in a list.

Solution

  1. Step 1: Understand the purpose of Sort-Object

    Sort-Object is used to order or arrange objects based on their properties or values.
  2. Step 2: Compare options with the cmdlet's function

    Only It arranges objects in a specified order based on property values. correctly describes arranging objects in order. Other options describe unrelated actions.
  3. Final Answer:

    It arranges objects in a specified order based on property values. -> Option B
  4. Quick Check:

    Sort-Object orders data = C [OK]
Hint: Sort-Object means arranging data in order [OK]
Common Mistakes:
  • Confusing sorting with deleting or counting
  • Thinking Sort-Object creates new objects
  • Assuming it only works on numbers
2. Which of the following is the correct syntax to sort a list of numbers stored in $numbers in descending order?
easy
A. $numbers | Sort-Object Descending
B. $numbers | Sort-Object /Descending
C. $numbers | Sort-Object -descend
D. $numbers | Sort-Object -Descending

Solution

  1. Step 1: Recall the correct parameter for descending order

    The correct parameter is -Descending with a dash and full word.
  2. Step 2: Check each option's syntax

    $numbers | Sort-Object -Descending uses the correct dash and spelling. Options A, C, and D have syntax errors or wrong parameter format.
  3. Final Answer:

    $numbers | Sort-Object -Descending -> Option D
  4. Quick Check:

    Use dash and full '-Descending' [OK]
Hint: Use dash before parameters like -Descending [OK]
Common Mistakes:
  • Omitting the dash before parameters
  • Using incorrect parameter spelling
  • Using slashes instead of dashes
3. What is the output of this PowerShell code?
$data = @( @{Name='John'; Age=30}, @{Name='Anna'; Age=25}, @{Name='Mike'; Age=35} )
$data | Sort-Object Age | ForEach-Object { $_.Name }
medium
A. Anna, John, Mike
B. John, Anna, Mike
C. Mike, John, Anna
D. Anna, Mike, John

Solution

  1. Step 1: Understand sorting by Age property

    The code sorts the array of objects by the Age property in ascending order: 25, 30, 35.
  2. Step 2: Map sorted objects to their Name property

    After sorting, the names in order are Anna (25), John (30), Mike (35).
  3. Final Answer:

    Anna, John, Mike -> Option A
  4. Quick Check:

    Sort by Age ascending = Anna, John, Mike [OK]
Hint: Sort-Object defaults to ascending order [OK]
Common Mistakes:
  • Assuming Sort-Object sorts descending by default
  • Mixing up property names
  • Not accessing the Name property after sorting
4. You run this code but get an error:
$items = @(5, 3, 8, 1)
$items | Sort-Object -descendingg

What is the problem?
medium
A. You must use parentheses around the array.
B. Sort-Object cannot sort numbers.
C. The parameter '-descendingg' is misspelled.
D. The pipe operator '|' is not allowed here.

Solution

  1. Step 1: Check the parameter spelling

    The parameter should be -Descending, but it is misspelled as -descendingg.
  2. Step 2: Understand impact of misspelling

    PowerShell does not recognize the misspelled parameter, causing an error.
  3. Final Answer:

    The parameter '-descendingg' is misspelled. -> Option C
  4. Quick Check:

    Parameter spelling must be exact [OK]
Hint: Check parameter spelling carefully [OK]
Common Mistakes:
  • Misspelling parameters
  • Thinking Sort-Object can't sort numbers
  • Misunderstanding pipe usage
5. You have this list of objects:
$people = @( @{Name='Sam'; Score=90}, @{Name='Liz'; Score=90}, @{Name='Tom'; Score=85} )

You want to sort by Score descending, but if scores are equal, sort by Name ascending. Which command achieves this?
hard
A. $people | Sort-Object -Property @{Expression='Score';Descending=$true}, @{Expression='Name';Descending=$false}
B. $people | Sort-Object Name, Score -Descending
C. $people | Sort-Object Score -Descending | Sort-Object Name
D. $people | Sort-Object -Property Score, Name -Descending

Solution

  1. Step 1: Understand sorting by multiple properties with different orders

    You want Score sorted descending and Name sorted ascending when scores tie.
  2. Step 2: Analyze each option's approach

    $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.
  3. Final Answer:

    $people | Sort-Object -Property @{Expression='Score';Descending=$true}, @{Expression='Name';Descending=$false} -> Option A
  4. Quick Check:

    Use script blocks for mixed ascending/descending [OK]
Hint: Use script blocks to mix ascending and descending [OK]
Common Mistakes:
  • Applying -Descending to all properties at once
  • Sorting twice losing original order
  • Sorting properties in wrong priority order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard