January 2018
Intermediate to advanced
434 pages
14h 1m
English
The sortedBy function is syntactic sugar provided by Kotlin. Internally, it's calling the sortedWith method that takes in a comparator.
Now, let's see the implementation of the sortBy function:
public inline fun <T, R : Comparable<R>> Iterable<T>.sortedBy(crossinline selector: (T) -> R?): List<T> { return sortedWith(compareBy(selector))}
The sortBy function calls the sortedWith method inside it, which is as following:
public fun <T> Iterable<T>.sortedWith(comparator: Comparator<in T>): List<T> { if (this is Collection) { if (size <= 1) return this.toList() @Suppress("UNCHECKED_CAST") return (toTypedArray<Any?>() as Array<T>).apply { sortWith(comparator) }.asList() } return toMutableList().apply { sortWith(comparator) }}
Read now
Unlock full access