kotlin sortedby

data class Person(val name: String, val age: Int)

fun main() {
    val people = listOf(
        Person("Alice", 30),
        Person("Bob", 25),
        Person("Charlie", 35)
    )

    val sortedPeople = people.sortedBy { it.age }

    sortedPeople.forEach { println(it) }
}
  1. Define a data class Person with properties name and age.
  2. Create a list of Person objects named people with different names and ages.
  3. Use the sortedBy function to sort the people list based on the age property in ascending order.
  4. Store the sorted list in the variable sortedPeople.
  5. Iterate over the sorted list using forEach and print each Person object.