KotlinCollection OpssortedBy
groupBy

sortedByCollection Ops

distinctBy

Returns a list of all elements sorted according to natural sort order of the selector.

Syntax

collection.sortedBy { selector }

Example

Enter values below to update the example in real time.

data
class
Person
val
name
String
people
listOf
println
sortedBy
it
Alice
Charlie
Bob
data class Person(val name: String, val age: Int)
val people = listOf(Person("Charlie", 30), Person("Alice", 25), Person("Bob", 35))
println(people.sortedBy { it.age })
// [Person(name=Alice, age=25), Person(name=Charlie, age=30), Person(name=Bob, age=35)]