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.
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 }
| Step | Action | Input List | Sorting Method | Resulting List |
|---|---|---|---|---|
| 1 | Start with numbers list | [3, 1, 4] | N/A | [3, 1, 4] |
| 2 | Call sorted() on numbers | [3, 1, 4] | sorted() | [1, 3, 4] |
| 3 | Start with people list | [Person(Bob,25), Person(Alice,30)] | N/A | [Bob(25), Alice(30)] |
| 4 | Call sortedBy { it.age } on people | [Bob(25), Alice(30)] | sortedBy { it.age } | [Bob(25), Alice(30)] |
| 5 | If reversed order needed, use sortedDescending or sortedByDescending | N/A | N/A | N/A |
| Variable | Start | After sorted() | After sortedBy() |
|---|---|---|---|
| numbers | [3, 1, 4] | [3, 1, 4] | [3, 1, 4] |
| sortedNumbers | N/A | [1, 3, 4] | N/A |
| people | [Bob(25), Alice(30)] | [Bob(25), Alice(30)] | [Bob(25), Alice(30)] |
| sortedByAge | N/A | N/A | [Bob(25), Alice(30)] |
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.sorted() function do in Kotlin?sorted()sorted() function creates a new list where elements are arranged in natural ascending order (numbers from smallest to largest, strings alphabetically).sorted() returns a new sorted list without modifying the original.sortedBy in Kotlin?sortedBy function takes a lambda expression that returns the property to sort by. The correct syntax is list.sortedBy { it.property }.{ it.length } which correctly accesses the length property of each string. Other options have syntax errors or incorrect usage.val numbers = listOf(5, 3, 8, 1)
val sortedNumbers = numbers.sortedBy { it % 3 }
println(sortedNumbers)it % 3 for each number: 5 % 3 = 2, 3 % 3 = 0, 8 % 3 = 2, 1 % 3 = 1.val fruits = listOf("apple", "banana", "cherry")
val sortedFruits = fruits.sortedBy(it.length)
println(sortedFruits)sortedBy function requires a lambda expression inside curly braces, like { it.length }. The code uses parentheses without braces, which is incorrect.data class Person(val name: String, val age: Int), how would you sort the list first by age ascending, then by name alphabetically using Kotlin's sorting functions?sortedWith(compareBy(...)) passing lambdas for each property in priority order.compareBy with age first, then name. persons.sortedBy { it.age + it.name } incorrectly adds age and name (different types). persons.sortedBy { it.name }.sortedBy { it.age } sorts twice but second sort overrides first. persons.sortedByDescending { it.age }.sortedBy { it.name } sorts age descending, not ascending.Pass the test and this pattern moves unknown