0
0
Kotlinprogramming~15 mins

Sorted and sortedBy in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Sorting a List of Fruits by Name and Length
📖 Scenario: You work in a fruit store and want to organize your list of fruits. Sometimes you want to see them in alphabetical order, and other times by how long their names are.
🎯 Goal: Learn how to use sorted() to sort a list alphabetically and sortedBy() to sort by a specific property, like the length of the fruit names.
📋 What You'll Learn
Create a list of fruits with exact names
Create a variable to hold the sorted list alphabetically
Create a variable to hold the list sorted by length of fruit names
Print both sorted lists exactly as specified
💡 Why This Matters
🌍 Real World
Sorting lists is common in apps like shopping lists, contact lists, or any place where you want to organize items for easier viewing.
💼 Career
Understanding sorting helps in data processing, UI display, and backend logic in many software development roles.
Progress0 / 4 steps
1
Create the list of fruits
Create a list called fruits with these exact string values in this order: "Banana", "Apple", "Cherry", "Date", "Elderberry"
Kotlin
Need a hint?

Use listOf() to create a list with the exact fruit names.

2
Create a variable for alphabetical sorting
Create a variable called sortedFruits that holds the list fruits sorted alphabetically using sorted()
Kotlin
Need a hint?

Use sorted() on the fruits list to get an alphabetically sorted list.

3
Create a variable for sorting by length
Create a variable called sortedByLength that holds the list fruits sorted by the length of each fruit name using sortedBy and the lambda { it.length }
Kotlin
Need a hint?

Use sortedBy { it.length } to sort the list by the length of each fruit name.

4
Print both sorted lists
Print the variable sortedFruits on one line and then print sortedByLength on the next line exactly as lists (use println)
Kotlin
Need a hint?

Use println() to print each sorted list on its own line.