Recall & Review
beginner
What does the
sorted() function do in Kotlin?It returns a new list with the elements sorted in natural ascending order without changing the original list.
Click to reveal answer
beginner
How does
sortedBy differ from sorted()?sortedBy sorts elements based on a selector function you provide, allowing custom sorting criteria.Click to reveal answer
beginner
Example: What is the result of
listOf(3, 1, 2).sorted()?The result is
[1, 2, 3], a new list sorted in ascending order.Click to reveal answer
beginner
Example: How to sort a list of strings by their length using
sortedBy?Use
list.sortedBy { it.length } to get a list sorted by string length.Click to reveal answer
beginner
Does
sorted() or sortedBy modify the original list?No, both return a new sorted list and leave the original list unchanged.
Click to reveal answer
What does
sorted() return when called on a list?✗ Incorrect
sorted() returns a new list sorted in ascending order without changing the original.Which function lets you sort a list by a custom property?
✗ Incorrect
sortedBy() sorts elements using a selector function you provide.What will
listOf("apple", "kiwi", "banana").sortedBy { it.length } return?✗ Incorrect
The list is sorted by string length: "kiwi" (4), "apple" (5), "banana" (6).
Does
sorted() change the original list?✗ Incorrect
sorted() always returns a new sorted list and does not modify the original.Which of these is true about
sortedBy?✗ Incorrect
sortedBy sorts elements based on a key selector function you provide.Explain how
sorted() and sortedBy() work in Kotlin and when you would use each.Think about sorting numbers vs sorting by a property like length or age.
You got /5 concepts.
Describe a real-life example where
sortedBy is more useful than sorted().Imagine sorting a list of fruits by how long their names are.
You got /4 concepts.