Complete the code to sort the list in ascending order.
val numbers = listOf(5, 3, 8, 1) val sortedNumbers = numbers.[1]()
The sorted() function returns a new list with elements sorted in ascending order.
Complete the code to sort the list of strings by their length.
val words = listOf("apple", "banana", "kiwi", "pear") val sortedWords = words.[1] { it.length }
The sortedBy function sorts elements according to the value returned by the selector function, here it.length.
Fix the error in the code to sort the list of pairs by the second value.
val pairs = listOf(Pair("a", 3), Pair("b", 1), Pair("c", 2)) val sortedPairs = pairs.[1] { it.second }
sortedBy returns a new sorted list by the selector. sortBy works only on mutable lists.
Fill both blanks to create a map of words to their lengths, including only words longer than 3 characters.
val words = listOf("cat", "elephant", "dog", "horse") val wordLengths = { [1] : [2] for word in words if word.length > 3 }
The map keys are the words themselves, and the values are their lengths using word.length.
Fill all three blanks to create a map of uppercase words to their lengths, including only words longer than 4 characters.
val words = listOf("apple", "bat", "carrot", "dog") val result = { [1] : [2] for word in words if word.[3] 4 }
The key is the uppercase word using word.uppercase(), the value is the length with word.length, and the condition checks word.length > 4.