[kotlin] 리스트(List)의 요소를 정렬하는 방법은 무엇인가요?

예를 들어, 다음은 리스트를 오름차순으로 정렬하는 방법입니다:

val unsortedList = listOf(3, 1, 2, 5, 4)
val sortedList = unsortedList.sorted()
println(sortedList) // 출력: [1, 2, 3, 4, 5]

정렬된 새로운 리스트를 얻고 싶지 않고 기존의 리스트를 변경하고 싶다면, sorted() 대신 sortedInPlace() 함수를 사용할 수 있습니다.

내림차순으로 리스트를 정렬하려면 다음과 같이 사용합니다:

val unsortedList = listOf(3, 1, 2, 5, 4)
val sortedList = unsortedList.sortedDescending()
println(sortedList) // 출력: [5, 4, 3, 2, 1]

자세한 내용은 Kotlin 공식 문서를 참조하세요.