0
0
Kotlinprogramming~5 mins

Sorted and sortedBy in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA new list sorted in ascending order
BThe original list sorted in place
CA list sorted in descending order
DAn error if the list is empty
Which function lets you sort a list by a custom property?
Asorted()
Bsort()
Cfilter()
DsortedBy()
What will listOf("apple", "kiwi", "banana").sortedBy { it.length } return?
A["banana", "apple", "kiwi"]
B["apple", "kiwi", "banana"]
C["kiwi", "apple", "banana"]
D["kiwi", "banana", "apple"]
Does sorted() change the original list?
AYes, it sorts the original list
BNo, it returns a new sorted list
COnly if the list is mutable
DOnly if you assign it back
Which of these is true about sortedBy?
AIt sorts elements by a key you specify
BIt only works on numbers
CIt sorts elements randomly
DIt sorts elements by their natural order
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.