KJS: reimplement collectionSort in Kotlin
This commit is contained in:
@@ -80,17 +80,24 @@ public fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = hashMapOf(pair)
|
|||||||
* Sorts elements in the list in-place according to their natural sort order.
|
* Sorts elements in the list in-place according to their natural sort order.
|
||||||
*/
|
*/
|
||||||
public fun <T : Comparable<T>> MutableList<T>.sort(): Unit {
|
public fun <T : Comparable<T>> MutableList<T>.sort(): Unit {
|
||||||
if (size > 1) collectionsSort(this, naturalOrder())
|
collectionsSort(this, naturalOrder())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sorts elements in the list in-place according to the order specified with [comparator].
|
* Sorts elements in the list in-place according to the order specified with [comparator].
|
||||||
*/
|
*/
|
||||||
public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
|
public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
|
||||||
if (size > 1) collectionsSort(this, comparator)
|
collectionsSort(this, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
@library("collectionsSort")
|
private fun <T> collectionsSort(list: MutableList<T>, comparator: Comparator<in T>) {
|
||||||
private fun <T> collectionsSort(list: MutableList<T>, comparator: Comparator<in T>): Unit = noImpl
|
if (list.size <= 1) return
|
||||||
|
|
||||||
|
val array = copyToArray(list)
|
||||||
|
|
||||||
|
array.asDynamic().sort(comparator.asDynamic().compare.bind(comparator))
|
||||||
|
|
||||||
|
for (i in 0..array.size - 1) {
|
||||||
|
list[i] = array[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -274,23 +274,6 @@ Kotlin.arrayDeepHashCode = function (arr) {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
Kotlin.collectionsSort = function (mutableList, comparator) {
|
|
||||||
var boundComparator = void 0;
|
|
||||||
if (comparator !== void 0) {
|
|
||||||
boundComparator = comparator.compare.bind(comparator);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mutableList.size > 1) {
|
|
||||||
var array = _.kotlin.collections.copyToArray(mutableList);
|
|
||||||
|
|
||||||
array.sort(boundComparator);
|
|
||||||
|
|
||||||
for (var i = 0, n = array.length; i < n; i++) {
|
|
||||||
mutableList.set_vux3hl$(i, array[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Kotlin.primitiveArraySort = function(array) {
|
Kotlin.primitiveArraySort = function(array) {
|
||||||
array.sort(Kotlin.primitiveCompareTo)
|
array.sort(Kotlin.primitiveCompareTo)
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user