diff --git a/js/js.libraries/src/core/collections.kt b/js/js.libraries/src/core/collections.kt index 2b904daf902..554a71aebbf 100644 --- a/js/js.libraries/src/core/collections.kt +++ b/js/js.libraries/src/core/collections.kt @@ -80,17 +80,24 @@ public fun mapOf(pair: Pair): Map = hashMapOf(pair) * Sorts elements in the list in-place according to their natural sort order. */ public fun > MutableList.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 MutableList.sortWith(comparator: Comparator): Unit { - if (size > 1) collectionsSort(this, comparator) + collectionsSort(this, comparator) } -@library("collectionsSort") -private fun collectionsSort(list: MutableList, comparator: Comparator): Unit = noImpl +private fun collectionsSort(list: MutableList, comparator: Comparator) { + 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] + } +} diff --git a/js/js.libraries/src/js/kotlin_lib.js b/js/js.libraries/src/js/kotlin_lib.js index 5d161affa9b..a9e46a828fb 100644 --- a/js/js.libraries/src/js/kotlin_lib.js +++ b/js/js.libraries/src/js/kotlin_lib.js @@ -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) };