Provide stable sorting when JS engine sorting doesn't look stable

#KT-12473
This commit is contained in:
Ilya Gorbunov
2019-01-10 19:29:53 +03:00
parent 7c3c454654
commit 51eb21d44b
5 changed files with 176 additions and 31 deletions
@@ -1159,8 +1159,7 @@ public actual fun IntArray.sort(): Unit {
* Sorts the array in-place.
*/
public actual fun LongArray.sort(): Unit {
if (size > 1)
sort { a: Long, b: Long -> a.compareTo(b) }
if (size > 1) sort { a: Long, b: Long -> a.compareTo(b) }
}
/**
@@ -1207,16 +1206,14 @@ public actual fun CharArray.sort(): Unit {
* Sorts the array in-place according to the natural order of its elements.
*/
public actual fun <T : Comparable<T>> Array<out T>.sort(): Unit {
if (size > 1)
sort { a: T, b: T -> a.compareTo(b) }
if (size > 1) sortArray(this)
}
/**
* Sorts the array in-place according to the order specified by the given [comparison] function.
*/
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.sort(noinline comparison: (a: T, b: T) -> Int): Unit {
asDynamic().sort(comparison)
public fun <T> Array<out T>.sort(comparison: (a: T, b: T) -> Int): Unit {
if (size > 1) sortArrayWith(this, comparison)
}
/**
@@ -1279,8 +1276,7 @@ public inline fun CharArray.sort(noinline comparison: (a: Char, b: Char) -> Int)
* Sorts the array in-place according to the order specified by the given [comparator].
*/
public actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit {
if (size > 1)
sort { a, b -> comparator.compare(a, b) }
if (size > 1) sortArrayWith(this, comparator)
}
/**