KJS: reimplement collectionSort in Kotlin

This commit is contained in:
Zalim Bashorov
2016-12-24 23:40:23 +03:00
parent 20c482598a
commit d985d30f91
2 changed files with 11 additions and 21 deletions
+11 -4
View File
@@ -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.
*/
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].
*/
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>): Unit = noImpl
private fun <T> collectionsSort(list: MutableList<T>, comparator: Comparator<in T>) {
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]
}
}
-17
View File
@@ -274,23 +274,6 @@ Kotlin.arrayDeepHashCode = function (arr) {
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) {
array.sort(Kotlin.primitiveCompareTo)
};