Rewrite MutableList.sortWith to suppress warning instead of error

Use available naturalOrder comparator in MutableList.sort()
This commit is contained in:
Ilya Gorbunov
2018-12-27 18:34:19 +03:00
committed by Vasily Levchenko
parent d74384db18
commit 58000a20fd
@@ -5,14 +5,12 @@
package kotlin.collections
import kotlin.comparisons.*
/**
* Sorts elements in the list in-place according to their natural sort order.
*
* The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting.
*/
public actual fun <T : Comparable<T>> MutableList<T>.sort(): Unit = sortWith(Comparator<T> { a: T, b: T -> a.compareTo(b) })
public actual fun <T : Comparable<T>> MutableList<T>.sort(): Unit = sortWith(naturalOrder())
/**
* Sorts elements in the list in-place according to the order specified with [comparator].
@@ -22,7 +20,7 @@ public actual fun <T : Comparable<T>> MutableList<T>.sort(): Unit = sortWith(Com
public actual fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
if (size > 1) {
val it = listIterator()
val sortedArray = @Suppress("TYPE_PARAMETER_AS_REIFIED") toTypedArray().apply { sortWith(comparator) }
val sortedArray = @Suppress("UNCHECKED_CAST") (toTypedArray<Any?>() as Array<T>).apply { sortWith(comparator) }
for (v in sortedArray) {
it.next()
it.set(v)