sorted
public fun <T : Comparable<T>> Iterable<T>.sorted(): List<T> {
if (this is Collection) {
if (size <= 1) return this.toList()
@Suppress("UNCHECKED_CAST")
return (toTypedArray<Comparable<T>>() as Array<T>).apply{ sort() }.asList() }
return toMutableList().apply { sort() }
}
val list = listOf(1, 4, 2, 3, 5)
println(list.sorted()) // [1, 2, 3, 4, 5]
Last updated