0
0
Kotlinprogramming~10 mins

Sorted and sortedBy in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to sort the list in ascending order.

Kotlin
val numbers = listOf(5, 3, 8, 1)
val sortedNumbers = numbers.[1]()
Drag options to blanks, or click blank then click option'
AorderBy
Bsort
Csorted
Darrange
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sort' which is not a function on immutable lists.
Using 'orderBy' or 'arrange' which are not Kotlin functions.
2fill in blank
medium

Complete the code to sort the list of strings by their length.

Kotlin
val words = listOf("apple", "banana", "kiwi", "pear")
val sortedWords = words.[1] { it.length }
Drag options to blanks, or click blank then click option'
AsortedBy
Bsorted
CsortBy
DorderBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sorted' which sorts by natural order, not by length.
Using 'sortBy' which is a mutable list function.
3fill in blank
hard

Fix the error in the code to sort the list of pairs by the second value.

Kotlin
val pairs = listOf(Pair("a", 3), Pair("b", 1), Pair("c", 2))
val sortedPairs = pairs.[1] { it.second }
Drag options to blanks, or click blank then click option'
Asort
BsortedBy
Csorted
DsortBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sortBy' on an immutable list causes an error.
Using 'sorted' without a selector sorts by natural order.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, including only words longer than 3 characters.

Kotlin
val words = listOf("cat", "elephant", "dog", "horse")
val wordLengths = { [1] : [2] for word in words if word.length > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.length
Dword.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len(word)' which is Python syntax, not Kotlin.
Using 'word.size' which is not a property of String in Kotlin.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths, including only words longer than 4 characters.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val result = { [1] : [2] for word in words if word.[3] 4 }
Drag options to blanks, or click blank then click option'
Aword.uppercase()
Bword.length
Clength
Dlength()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length()' which is not a Kotlin property but a method in some languages.
Using 'word.length()' which is invalid in Kotlin.