Let's say we have a class Person that contains the name and age fields. We need to sort the list of objects of this class by age in descending order.
Comparable and Comparator
From top to bottom
Report a typo
Sample Input 1:
Sample Output 1:
Alice, 40
Sarah, 30
John, 25
David, 20Write a program in Kotlin
data class Person(val name: String, val age: Int)
fun main() {
val people = listOf(
Person("John", 25),
Person("Sarah", 30),
Person("David", 20),
Person("Alice", 40)
)
val comparator = // write your code here
val sortedPeople = people.sortedWith(comparator)
sortedPeople.forEach { println("${it.name}, ${it.age}") }
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.