Kotlin리스트sortedBy

sortedBy리스트

셀렉터 기준 오름차순 정렬된 새 리스트를 반환합니다.

문법

list.sortedBy { selector }

예제

아래 값을 입력하면 예제에 즉시 반영됩니다.

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)]