0
0
Swiftprogramming~10 mins

Sorted and custom comparators in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sorted and custom comparators
Start with array
Call sorted()
Check if custom comparator provided?
NoUse default ascending order
Yes
Use custom comparator to compare elements
Sort elements accordingly
Return sorted array
End
The array is sorted by calling sorted(). If a custom comparator is given, it compares elements using that; otherwise, it sorts in ascending order.
Execution Sample
Swift
let numbers = [3, 1, 4, 2]
let sortedAsc = numbers.sorted()
let sortedDesc = numbers.sorted { $0 > $1 }
Sorts an array ascending by default, then descending using a custom comparator.
Execution Table
StepActionComparator UsedComparison ExampleResulting Array
1Start with arrayN/AN/A[3, 1, 4, 2]
2Call sorted() without comparatorDefault ascendingCompare 3 and 1: 3 > 1[1, 2, 3, 4]
3Call sorted() with custom comparatorCustom: $0 > $1 (descending)Compare 3 and 1: 3 > 1 is true[4, 3, 2, 1]
4EndN/AN/ASorted arrays ready
💡 Sorting completes after comparing all elements using the chosen comparator.
Variable Tracker
VariableStartAfter sorted()After sorted { $0 > $1 }
numbers[3, 1, 4, 2][3, 1, 4, 2][3, 1, 4, 2]
sortedAscN/A[1, 2, 3, 4][1, 2, 3, 4]
sortedDescN/AN/A[4, 3, 2, 1]
Key Moments - 3 Insights
Why does sorted() without a comparator sort in ascending order?
By default, sorted() uses the natural order of elements (like numbers from smallest to largest), as shown in step 2 of the execution_table.
How does the custom comparator { $0 > $1 } change sorting?
It tells sorted() to put bigger numbers first by comparing if the first element is greater than the second, reversing the order (step 3).
Does the original array change after sorting?
No, sorted() returns a new sorted array and leaves the original array unchanged, as seen in variable_tracker where 'numbers' stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array after calling sorted() without a comparator?
A[4, 3, 2, 1]
B[1, 2, 3, 4]
C[3, 1, 4, 2]
D[2, 3, 1, 4]
💡 Hint
Check step 2 in the execution_table under 'Resulting Array'.
At which step does the sorting use the custom comparator?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the step mentioning 'custom comparator' in the execution_table.
If the comparator was changed to { $0 < $1 }, how would the sorted array change?
AIt would sort descending
BIt would not sort
CIt would sort ascending
DIt would reverse the array
💡 Hint
The comparator { $0 < $1 } means smaller elements come first, like the default ascending order.
Concept Snapshot
sorted() returns a new sorted array.
By default, it sorts ascending.
Custom comparator closure defines order.
Comparator returns true if first element should come before second.
Original array stays unchanged.
Use sorted(by:) for custom sorting.
Full Transcript
This example shows how to sort arrays in Swift using sorted(). First, the array [3, 1, 4, 2] is sorted ascending by default, producing [1, 2, 3, 4]. Then, a custom comparator { $0 > $1 } is used to sort descending, producing [4, 3, 2, 1]. The original array remains unchanged. The flow starts with the array, calls sorted(), checks if a custom comparator is provided, uses it if yes, sorts accordingly, and returns the sorted array. Variables track the original and sorted arrays. Key points include default ascending order, how the comparator changes sorting, and that sorted() returns a new array without modifying the original.