diff --git a/js/js.libraries/src/core/kotlin_special.kt b/js/js.libraries/src/core/kotlin_special.kt index 79c108b27f2..2b392eb4180 100644 --- a/js/js.libraries/src/core/kotlin_special.kt +++ b/js/js.libraries/src/core/kotlin_special.kt @@ -489,7 +489,7 @@ public inline fun ShortArray.plus(element: Short): ShortArray { } /** - * Sorts the array inplace. + * Sorts the array in-place. */ @library("primitiveArraySort") public fun ByteArray.sort(): Unit { @@ -497,7 +497,7 @@ public fun ByteArray.sort(): Unit { } /** - * Sorts the array inplace. + * Sorts the array in-place. */ @library("primitiveArraySort") public fun CharArray.sort(): Unit { @@ -505,7 +505,7 @@ public fun CharArray.sort(): Unit { } /** - * Sorts the array inplace. + * Sorts the array in-place. */ @library("primitiveArraySort") public fun DoubleArray.sort(): Unit { @@ -513,7 +513,7 @@ public fun DoubleArray.sort(): Unit { } /** - * Sorts the array inplace. + * Sorts the array in-place. */ @library("primitiveArraySort") public fun FloatArray.sort(): Unit { @@ -521,7 +521,7 @@ public fun FloatArray.sort(): Unit { } /** - * Sorts the array inplace. + * Sorts the array in-place. */ @library("primitiveArraySort") public fun IntArray.sort(): Unit { @@ -529,7 +529,7 @@ public fun IntArray.sort(): Unit { } /** - * Sorts the array inplace. + * Sorts the array in-place. */ @library("primitiveArraySort") public fun ShortArray.sort(): Unit { @@ -537,21 +537,23 @@ public fun ShortArray.sort(): Unit { } /** - * Sorts the array inplace. + * Sorts the array in-place. */ public fun > Array.sort(): Unit { - sort { a: T, b: T -> a.compareTo(b) } + if (size > 1) + sort { a: T, b: T -> a.compareTo(b) } } /** - * Sorts the array inplace. + * Sorts the array in-place. */ public fun LongArray.sort(): Unit { - sort { a: Long, b: Long -> a.compareTo(b) } + if (size > 1) + sort { a: Long, b: Long -> a.compareTo(b) } } /** - * Sorts the array inplace according to the order specified by the given [comparison] function. + * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native public fun Array.sort(comparison: (T, T) -> Int): Unit { @@ -559,7 +561,7 @@ public fun Array.sort(comparison: (T, T) -> Int): Unit { } /** - * Sorts the array inplace according to the order specified by the given [comparison] function. + * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native public fun ByteArray.sort(comparison: (Byte, Byte) -> Int): Unit { @@ -567,7 +569,7 @@ public fun ByteArray.sort(comparison: (Byte, Byte) -> Int): Unit { } /** - * Sorts the array inplace according to the order specified by the given [comparison] function. + * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native public fun CharArray.sort(comparison: (Char, Char) -> Int): Unit { @@ -575,7 +577,7 @@ public fun CharArray.sort(comparison: (Char, Char) -> Int): Unit { } /** - * Sorts the array inplace according to the order specified by the given [comparison] function. + * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native public fun DoubleArray.sort(comparison: (Double, Double) -> Int): Unit { @@ -583,7 +585,7 @@ public fun DoubleArray.sort(comparison: (Double, Double) -> Int): Unit { } /** - * Sorts the array inplace according to the order specified by the given [comparison] function. + * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native public fun FloatArray.sort(comparison: (Float, Float) -> Int): Unit { @@ -591,7 +593,7 @@ public fun FloatArray.sort(comparison: (Float, Float) -> Int): Unit { } /** - * Sorts the array inplace according to the order specified by the given [comparison] function. + * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native public fun IntArray.sort(comparison: (Int, Int) -> Int): Unit { @@ -599,7 +601,7 @@ public fun IntArray.sort(comparison: (Int, Int) -> Int): Unit { } /** - * Sorts the array inplace according to the order specified by the given [comparison] function. + * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native public fun LongArray.sort(comparison: (Long, Long) -> Int): Unit { @@ -607,7 +609,7 @@ public fun LongArray.sort(comparison: (Long, Long) -> Int): Unit { } /** - * Sorts the array inplace according to the order specified by the given [comparison] function. + * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native public fun ShortArray.sort(comparison: (Short, Short) -> Int): Unit { @@ -615,10 +617,11 @@ public fun ShortArray.sort(comparison: (Short, Short) -> Int): Unit { } /** - * Sorts the array inplace according to the order specified by the given [comparator] object. + * Sorts the array in-place according to the order specified by the given [comparator] object. */ public fun Array.sortWith(comparator: Comparator): Unit { - sort { a, b -> comparator.compare(a, b) } + if (size > 1) + sort { a, b -> comparator.compare(a, b) } } /** diff --git a/js/js.translator/testData/kotlin_lib.js b/js/js.translator/testData/kotlin_lib.js index 6f1b0cb7032..c67ef286552 100644 --- a/js/js.translator/testData/kotlin_lib.js +++ b/js/js.translator/testData/kotlin_lib.js @@ -999,17 +999,15 @@ mutableList.sort(boundComparator); } - //TODO: should be deleted when List will be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects) - var array = []; - var it = mutableList.iterator(); - while (it.hasNext()) { - array.push(it.next()); - } + if (mutableList.size > 1) { + //TODO: should be deleted when List will be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects) + var array = Kotlin.copyToArray(mutableList); - array.sort(boundComparator); + array.sort(boundComparator); - for (var i = 0, n = array.length; i < n; i++) { - mutableList.set_vux3hl$(i, array[i]); + for (var i = 0, n = array.length; i < n; i++) { + mutableList.set_vux3hl$(i, array[i]); + } } }; diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index a296402a048..c7b271862b6 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -4725,56 +4725,56 @@ public fun ShortArray.sortDescending(): Unit { * Returns a list of all elements sorted according to their natural sort order. */ public fun > Array.sorted(): List { - return toArrayList().apply { sort() } + return sortedArray().asList() } /** * Returns a list of all elements sorted according to their natural sort order. */ public fun ByteArray.sorted(): List { - return toArrayList().apply { sort() } + return toTypedArray().apply { sort() }.asList() } /** * Returns a list of all elements sorted according to their natural sort order. */ public fun CharArray.sorted(): List { - return toArrayList().apply { sort() } + return toTypedArray().apply { sort() }.asList() } /** * Returns a list of all elements sorted according to their natural sort order. */ public fun DoubleArray.sorted(): List { - return toArrayList().apply { sort() } + return toTypedArray().apply { sort() }.asList() } /** * Returns a list of all elements sorted according to their natural sort order. */ public fun FloatArray.sorted(): List { - return toArrayList().apply { sort() } + return toTypedArray().apply { sort() }.asList() } /** * Returns a list of all elements sorted according to their natural sort order. */ public fun IntArray.sorted(): List { - return toArrayList().apply { sort() } + return toTypedArray().apply { sort() }.asList() } /** * Returns a list of all elements sorted according to their natural sort order. */ public fun LongArray.sorted(): List { - return toArrayList().apply { sort() } + return toTypedArray().apply { sort() }.asList() } /** * Returns a list of all elements sorted according to their natural sort order. */ public fun ShortArray.sorted(): List { - return toArrayList().apply { sort() } + return toTypedArray().apply { sort() }.asList() } /** @@ -5099,63 +5099,63 @@ public fun ShortArray.sortedDescending(): List { * Returns a list of all elements sorted according to the specified [comparator]. */ public fun Array.sortedWith(comparator: Comparator): List { - return toArrayList().apply { sortWith(comparator) } + return sortedArrayWith(comparator).asList() } /** * Returns a list of all elements sorted according to the specified [comparator]. */ public fun BooleanArray.sortedWith(comparator: Comparator): List { - return toArrayList().apply { sortWith(comparator) } + return toTypedArray().apply { sortWith(comparator) }.asList() } /** * Returns a list of all elements sorted according to the specified [comparator]. */ public fun ByteArray.sortedWith(comparator: Comparator): List { - return toArrayList().apply { sortWith(comparator) } + return toTypedArray().apply { sortWith(comparator) }.asList() } /** * Returns a list of all elements sorted according to the specified [comparator]. */ public fun CharArray.sortedWith(comparator: Comparator): List { - return toArrayList().apply { sortWith(comparator) } + return toTypedArray().apply { sortWith(comparator) }.asList() } /** * Returns a list of all elements sorted according to the specified [comparator]. */ public fun DoubleArray.sortedWith(comparator: Comparator): List { - return toArrayList().apply { sortWith(comparator) } + return toTypedArray().apply { sortWith(comparator) }.asList() } /** * Returns a list of all elements sorted according to the specified [comparator]. */ public fun FloatArray.sortedWith(comparator: Comparator): List { - return toArrayList().apply { sortWith(comparator) } + return toTypedArray().apply { sortWith(comparator) }.asList() } /** * Returns a list of all elements sorted according to the specified [comparator]. */ public fun IntArray.sortedWith(comparator: Comparator): List { - return toArrayList().apply { sortWith(comparator) } + return toTypedArray().apply { sortWith(comparator) }.asList() } /** * Returns a list of all elements sorted according to the specified [comparator]. */ public fun LongArray.sortedWith(comparator: Comparator): List { - return toArrayList().apply { sortWith(comparator) } + return toTypedArray().apply { sortWith(comparator) }.asList() } /** * Returns a list of all elements sorted according to the specified [comparator]. */ public fun ShortArray.sortedWith(comparator: Comparator): List { - return toArrayList().apply { sortWith(comparator) } + return toTypedArray().apply { sortWith(comparator) }.asList() } /** @@ -11447,7 +11447,71 @@ public operator fun Array.plus(element: T): Array { } /** - * Sorts array or range in array inplace. + * Sorts the array in-place. + */ +@kotlin.jvm.JvmVersion +public fun Array.sort(): Unit { + if (size > 1) Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +@kotlin.jvm.JvmVersion +public fun ByteArray.sort(): Unit { + if (size > 1) Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +@kotlin.jvm.JvmVersion +public fun CharArray.sort(): Unit { + if (size > 1) Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +@kotlin.jvm.JvmVersion +public fun DoubleArray.sort(): Unit { + if (size > 1) Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +@kotlin.jvm.JvmVersion +public fun FloatArray.sort(): Unit { + if (size > 1) Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +@kotlin.jvm.JvmVersion +public fun IntArray.sort(): Unit { + if (size > 1) Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +@kotlin.jvm.JvmVersion +public fun LongArray.sort(): Unit { + if (size > 1) Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +@kotlin.jvm.JvmVersion +public fun ShortArray.sort(): Unit { + if (size > 1) Arrays.sort(this) +} + +/** + * Sorts a range in the array in-place. */ @kotlin.jvm.JvmVersion public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { @@ -11455,7 +11519,7 @@ public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size()): Uni } /** - * Sorts array or range in array inplace. + * Sorts a range in the array in-place. */ @kotlin.jvm.JvmVersion public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { @@ -11463,7 +11527,7 @@ public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { } /** - * Sorts array or range in array inplace. + * Sorts a range in the array in-place. */ @kotlin.jvm.JvmVersion public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { @@ -11471,7 +11535,7 @@ public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { } /** - * Sorts array or range in array inplace. + * Sorts a range in the array in-place. */ @kotlin.jvm.JvmVersion public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { @@ -11479,7 +11543,7 @@ public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { } /** - * Sorts array or range in array inplace. + * Sorts a range in the array in-place. */ @kotlin.jvm.JvmVersion public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { @@ -11487,7 +11551,7 @@ public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { } /** - * Sorts array or range in array inplace. + * Sorts a range in the array in-place. */ @kotlin.jvm.JvmVersion public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { @@ -11495,7 +11559,7 @@ public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { } /** - * Sorts array or range in array inplace. + * Sorts a range in the array in-place. */ @kotlin.jvm.JvmVersion public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { @@ -11503,7 +11567,7 @@ public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { } /** - * Sorts array or range in array inplace. + * Sorts a range in the array in-place. */ @kotlin.jvm.JvmVersion public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { @@ -11511,7 +11575,15 @@ public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { } /** - * Sorts array or range in array inplace. + * Sorts the array in-place with the given [comparator]. + */ +@kotlin.jvm.JvmVersion +public fun Array.sortWith(comparator: Comparator): Unit { + if (size > 1) Arrays.sort(this, comparator) +} + +/** + * Sorts a range in the array in-place with the given [comparator]. */ @kotlin.jvm.JvmVersion public fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size()): Unit { diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 5e9d0d06139..c708e728a6e 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -848,6 +848,10 @@ public fun > MutableList.sortDescending(): Unit { * Returns a list of all elements sorted according to their natural sort order. */ public fun > Iterable.sorted(): List { + if (this is Collection) { + if (size <= 1) return this.toArrayList() + return (toTypedArray>() as Array).apply { sort() }.asList() + } return toArrayList().apply { sort() } } @@ -876,6 +880,10 @@ public fun > Iterable.sortedDescending(): List { * Returns a list of all elements sorted according to the specified [comparator]. */ public fun Iterable.sortedWith(comparator: Comparator): List { + if (this is Collection) { + if (size <= 1) return this.toArrayList() + return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() + } return toArrayList().apply { sortWith(comparator) } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index 46d20ccd730..03094595287 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -99,7 +99,21 @@ fun ordering(): List { typeParam("T : Comparable") body { """ - return toArrayList().apply { sort() } + if (this is Collection) { + if (size <= 1) return this.toArrayList() + return (toTypedArray>() as Array).apply { sort() }.asList() + } + return toArrayList().apply { sort() } + """ + } + body(ArraysOfPrimitives) { + """ + return toTypedArray().apply { sort() }.asList() + """ + } + body(ArraysOfObjects) { + """ + return sortedArray().asList() """ } @@ -214,9 +228,23 @@ fun ordering(): List { } body { """ + if (this is Collection) { + if (size <= 1) return this.toArrayList() + return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() + } return toArrayList().apply { sortWith(comparator) } """ } + body(ArraysOfPrimitives) { + """ + return toTypedArray().apply { sortWith(comparator) }.asList() + """ + } + body(ArraysOfObjects) { + """ + return sortedArrayWith(comparator).asList() + """ + } returns("SELF", Sequences) doc(Sequences) { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt index c63999d74bc..ebf06fe8349 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt @@ -136,7 +136,7 @@ fun specialJS(): List { exclude(PrimitiveType.Boolean) annotations("@native") returns("Unit") - doc { "Sorts the array inplace according to the order specified by the given [comparison] function." } + doc { "Sorts the array in-place according to the order specified by the given [comparison] function." } body { "return noImpl" } } @@ -144,8 +144,13 @@ fun specialJS(): List { only(ArraysOfObjects) exclude(PrimitiveType.Boolean) returns("Unit") - doc { "Sorts the array inplace according to the order specified by the given [comparator] object." } - body { "sort { a, b -> comparator.compare(a, b) }" } + doc { "Sorts the array in-place according to the order specified by the given [comparator] object." } + body { + """ + if (size > 1) + sort { a, b -> comparator.compare(a, b) } + """ + } } templates add f("sort()") { @@ -153,7 +158,7 @@ fun specialJS(): List { only(numericPrimitives + PrimitiveType.Char) exclude(PrimitiveType.Long) returns("Unit") - doc { "Sorts the array inplace." } + doc { "Sorts the array in-place." } annotations("""@library("primitiveArraySort")""") body { "return noImpl" } } @@ -163,10 +168,11 @@ fun specialJS(): List { only(PrimitiveType.Long) typeParam("T: Comparable") returns("Unit") - doc { "Sorts the array inplace." } + doc { "Sorts the array in-place." } body { """ - sort { a: T, b: T -> a.compareTo(b) } + if (size > 1) + sort { a: T, b: T -> a.compareTo(b) } """ } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index 37d2add89a0..ebe1314535d 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -119,24 +119,43 @@ fun specialJVM(): List { } } + + templates add f("sort()") { + only(ArraysOfObjects, ArraysOfPrimitives) + exclude(PrimitiveType.Boolean) + doc { "Sorts the array in-place." } + returns("Unit") + body { + "if (size > 1) Arrays.sort(this)" + } + } + templates add f("sort(fromIndex: Int = 0, toIndex: Int = size())") { only(ArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) - doc { "Sorts array or range in array inplace." } + doc { "Sorts a range in the array in-place." } returns("Unit") body { "Arrays.sort(this, fromIndex, toIndex)" } } + templates add f("sortWith(comparator: Comparator)") { + only(ArraysOfObjects) + doc { "Sorts the array in-place with the given [comparator]." } + returns("Unit") + body { + "if (size > 1) Arrays.sort(this, comparator)" + } + } + templates add f("sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size())") { only(ArraysOfObjects) - doc { "Sorts array or range in array inplace." } + doc { "Sorts a range in the array in-place with the given [comparator]." } returns("Unit") body { "Arrays.sort(this, fromIndex, toIndex, comparator)" } - } templates add f("filterIsInstanceTo(destination: C, klass: Class)") {