From 316fbd820b9f7ddc81b62d404407591802c4dbc5 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 2 Nov 2016 12:12:13 +0300 Subject: [PATCH] JS: drop Collections.sort --- js/js.libraries/src/core/collections.kt | 20 +++++++++++++++++++ .../src/core/javautilCollections.kt | 9 --------- .../kotlin/collections/MutableCollections.kt | 8 +++++--- libraries/stdlib/test/js/JsCollectionsTest.kt | 15 -------------- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/js/js.libraries/src/core/collections.kt b/js/js.libraries/src/core/collections.kt index 6ee2d2c25e4..83f52d80bd7 100644 --- a/js/js.libraries/src/core/collections.kt +++ b/js/js.libraries/src/core/collections.kt @@ -13,8 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package kotlin.collections +import java.util.Comparator +import kotlin.comparisons.naturalOrder @library("copyToArray") public fun Collection.toTypedArray(): Array = noImpl @@ -41,3 +44,20 @@ public fun setOf(element: T): Set = hashSetOf(element) * specified value. */ 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()) +} + +/** + * 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) +} + +@library("collectionsSort") +private fun collectionsSort(list: MutableList, comparator: Comparator): Unit = noImpl diff --git a/js/js.libraries/src/core/javautilCollections.kt b/js/js.libraries/src/core/javautilCollections.kt index ffa4db8641a..e2d6059a220 100644 --- a/js/js.libraries/src/core/javautilCollections.kt +++ b/js/js.libraries/src/core/javautilCollections.kt @@ -8,12 +8,6 @@ public object Collections { @Deprecated("Use collection.maxWith(comparator) instead.", ReplaceWith("col.maxWith(comp)")) public fun max(col: Collection, comp: Comparator): T = java.util.max(col, comp) - @Deprecated("Use list.sort() instead.", ReplaceWith("list.sort()")) - public fun > sort(list: MutableList): Unit = java.util.sort(list, naturalOrder()) - - @Deprecated("Use list.sortWith(comparator) instead.", ReplaceWith("list.sortWith(comparator)")) - public fun sort(list: MutableList, comparator: java.util.Comparator): Unit = java.util.sort(list, comparator) - @Deprecated("Use list.reverse() instead.", ReplaceWith("list.reverse()")) public fun reverse(list: MutableList): Unit { val size = list.size @@ -28,6 +22,3 @@ public object Collections { @library("collectionsMax") private fun max(col: Collection, comp: Comparator): T = noImpl - -@library("collectionsSort") -private fun sort(list: MutableList, comparator: Comparator): Unit = noImpl diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index dadd30dbe57..185e992f447 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -260,14 +260,16 @@ private fun MutableCollection<*>.retainNothing(): Boolean { /** * Sorts elements in the list in-place according to their natural sort order. - * */ -public fun > MutableList.sort(): Unit { + */ +@kotlin.jvm.JvmVersion +public fun > MutableList.sort(): Unit { if (size > 1) java.util.Collections.sort(this) } /** - * Sorts elements in the list in-place according to order specified with [comparator]. + * Sorts elements in the list in-place according to the order specified with [comparator]. */ +@kotlin.jvm.JvmVersion public fun MutableList.sortWith(comparator: Comparator): Unit { if (size > 1) java.util.Collections.sort(this, comparator) } diff --git a/libraries/stdlib/test/js/JsCollectionsTest.kt b/libraries/stdlib/test/js/JsCollectionsTest.kt index e72ee320454..4e56f6cca56 100644 --- a/libraries/stdlib/test/js/JsCollectionsTest.kt +++ b/libraries/stdlib/test/js/JsCollectionsTest.kt @@ -6,11 +6,8 @@ import kotlin.test.* import org.junit.Test as test import kotlin.comparisons.* -fun List.toArrayList() = this.toCollection(ArrayList()) - class JsCollectionsTest { val TEST_LIST = arrayOf(2, 0, 9, 7, 1).toList() - val SORTED_TEST_LIST = arrayOf(0, 1, 2, 7, 9).toList() val MAX_ELEMENT = 9 val COMPARATOR = Comparator { x: Int, y: Int -> if (x > y) 1 else if (x < y) -1 else 0 } @@ -18,18 +15,6 @@ class JsCollectionsTest { assertEquals(MAX_ELEMENT, Collections.max(TEST_LIST, COMPARATOR)) } - @test fun sort() { - val list = TEST_LIST.toArrayList() - Collections.sort(list) - assertEquals(SORTED_TEST_LIST, list) - } - - @test fun sortWithComparator() { - val list = TEST_LIST.toArrayList() - Collections.sort(list, COMPARATOR) - assertEquals(SORTED_TEST_LIST, list) - } - @test fun collectionToArray() { val array = TEST_LIST.toTypedArray() assertEquals(array.toList(), TEST_LIST)