From top to bottom

Report a typo

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.

Sample Input 1:

Sample Output 1:

Alice, 40
Sarah, 30
John, 25
David, 20
Write 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}") }
}
___

Create a free account to access the full topic