Sorting optimizations: do not make excessive copies, introduce methods to sort the whole array.

#KT-9904 Fixed
This commit is contained in:
Ilya Gorbunov
2015-11-05 19:12:32 +03:00
parent 3e81cdfc5d
commit 8bdd1e3246
7 changed files with 199 additions and 65 deletions
@@ -848,6 +848,10 @@ public fun <T : Comparable<T>> MutableList<T>.sortDescending(): Unit {
* Returns a list of all elements sorted according to their natural sort order.
*/
public fun <T : Comparable<T>> Iterable<T>.sorted(): List<T> {
if (this is Collection) {
if (size <= 1) return this.toArrayList()
return (toTypedArray<Comparable<T>>() as Array<T>).apply { sort() }.asList()
}
return toArrayList().apply { sort() }
}
@@ -876,6 +880,10 @@ public fun <T : Comparable<T>> Iterable<T>.sortedDescending(): List<T> {
* Returns a list of all elements sorted according to the specified [comparator].
*/
public fun <T> Iterable<T>.sortedWith(comparator: Comparator<in T>): List<T> {
if (this is Collection) {
if (size <= 1) return this.toArrayList()
return (toTypedArray<Any?>() as Array<T>).apply { sortWith(comparator) }.asList()
}
return toArrayList().apply { sortWith(comparator) }
}