sortedBy

public inline fun <T, R : Comparable<R>> Iterable<T>.sortedBy(crossinline
selector: (T) -> R?): List<T> {
    return sortedWith(compareBy(selector))
}

data class Person(val name: String, val age: Int)
val list = listOf(
    Person("gold", 20),
    Person("ari", 19),
    Person("kasadin", 50),
    Person("amumu", 15)
    )
    
println(list.sortedBy{ it.age })
// [Person(name=amumu, age=15), Person(name=ari, age=19), Person(name=gold, age=20), Person(name=kasadin, age=50)]

Last updated