0
0
Kotlinprogramming~10 mins

Sorted and sortedBy in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sorted and sortedBy
Start with List
Choose sorting method
sorted()
Sort ascending
Return new sorted list
End
Start with a list, choose sorted() for natural order or sortedBy() to sort by a property, then get a new sorted list.
Execution Sample
Kotlin
val numbers = listOf(3, 1, 4)
val sortedNumbers = numbers.sorted()
val people = listOf(Person("Bob", 25), Person("Alice", 30))
val sortedByAge = people.sortedBy { it.age }
Sort a list of numbers in ascending order and a list of people by their age.
Execution Table
StepActionInput ListSorting MethodResulting List
1Start with numbers list[3, 1, 4]N/A[3, 1, 4]
2Call sorted() on numbers[3, 1, 4]sorted()[1, 3, 4]
3Start with people list[Person(Bob,25), Person(Alice,30)]N/A[Bob(25), Alice(30)]
4Call sortedBy { it.age } on people[Bob(25), Alice(30)]sortedBy { it.age }[Bob(25), Alice(30)]
5If reversed order needed, use sortedDescending or sortedByDescendingN/AN/AN/A
💡 All sorting calls return new lists; original lists remain unchanged.
Variable Tracker
VariableStartAfter sorted()After sortedBy()
numbers[3, 1, 4][3, 1, 4][3, 1, 4]
sortedNumbersN/A[1, 3, 4]N/A
people[Bob(25), Alice(30)][Bob(25), Alice(30)][Bob(25), Alice(30)]
sortedByAgeN/AN/A[Bob(25), Alice(30)]
Key Moments - 3 Insights
Why does the original list 'numbers' not change after calling sorted()?
Because sorted() returns a new list and does not modify the original list, as shown in execution_table row 2.
How does sortedBy decide the order of elements?
sortedBy uses the key selector function (like 'it.age') to compare elements, as shown in execution_table row 4.
What if you want to sort in descending order?
Use sortedDescending() or sortedByDescending() to get a new list sorted in reverse order.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'sortedNumbers' after step 2?
A[3, 1, 4]
B[1, 3, 4]
C[4, 3, 1]
DN/A
💡 Hint
Check the 'Resulting List' column in row 2 of execution_table.
At which step does the sorting use a property of objects instead of natural order?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look for 'sortedBy { it.age }' in the 'Sorting Method' column.
If we changed sortedBy { it.age } to sortedByDescending { it.age }, how would the 'Resulting List' in step 4 change?
A[Alice(25), Bob(30)]
B[Bob(25), Alice(30)]
C[Alice(30), Bob(25)]
DNo change
💡 Hint
Descending order reverses the sorted list by the key property.
Concept Snapshot
sorted() returns a new list sorted in ascending natural order.
sortedBy { key } returns a new list sorted by the given key selector.
Original lists stay unchanged.
Use sortedDescending or sortedByDescending for reverse order.
Both return new lists, do not modify original.
Full Transcript
This visual shows how Kotlin's sorted() and sortedBy() work. We start with a list, then call sorted() to get a new list sorted in ascending order. The original list stays the same. For objects, sortedBy lets us sort by a property, like age. The result is a new list sorted by that property. We also note that descending order versions exist. Variables track original and new lists. Key moments clarify why original lists don't change and how sorting keys work. The quiz checks understanding of these steps.