// Created on savesnippets.com · https://savesnippets.com/Xi8pZhaoY19o93 data class User(val name: String, val age: Int, val signedUp: Long) fun main() { val users = listOf( User("Bob", 25, 100L), User("Alice", 30, 200L), User("Cara", 25, 50L), ) // Single field val byAge = users.sortedBy { it.age } val byAgeDesc = users.sortedByDescending { it.age } // Multiple fields — age asc, then name asc as tie-breaker val multi = users.sortedWith(compareBy({ it.age }, { it.name })) println(multi) // Mixed asc/desc — Comparator chain val complex = users.sortedWith( compareByDescending { it.age } .thenBy { it.signedUp } ) println(complex) // Pull the top N without fully sorting val newest3 = users.sortedByDescending { it.signedUp }.take(3) }