diff --git a/js/js.libraries/src/core/generated/_ArraysJs.kt b/js/js.libraries/src/core/generated/_ArraysJs.kt index 6891debb10e..2c9b86f3cd9 100644 --- a/js/js.libraries/src/core/generated/_ArraysJs.kt +++ b/js/js.libraries/src/core/generated/_ArraysJs.kt @@ -12850,6 +12850,78 @@ public inline fun CharArray.asList(): List { return this.unsafeCast>().asList() } +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun IntArray.sort(): Unit { + noImpl +} + +/** + * Sorts the array in-place. + */ +public fun LongArray.sort(): Unit { + if (size > 1) + sort { a: Long, b: Long -> a.compareTo(b) } +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun ByteArray.sort(): Unit { + noImpl +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun ShortArray.sort(): Unit { + noImpl +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun DoubleArray.sort(): Unit { + noImpl +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun FloatArray.sort(): Unit { + noImpl +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun CharArray.sort(): Unit { + noImpl +} + +/** + * Sorts the array in-place according to the natural order of its elements. + */ +public fun > Array.sort(): Unit { + if (size > 1) + sort { a: T, b: T -> a.compareTo(b) } +} + +/** + * Sorts the array in-place according to the order specified by the given [comparator]. + */ +public fun Array.sortWith(comparator: Comparator): Unit { + if (size > 1) + sort { a, b -> comparator.compare(a, b) } +} + /** * Returns a *typed* object array containing all of the elements of this primitive array. */ @@ -13328,70 +13400,6 @@ public inline fun Array.plusElement(element: T): Array { return this.asDynamic().concat(arrayOf(element)) } -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun ByteArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun ShortArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun IntArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun FloatArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun DoubleArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun CharArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -public fun > Array.sort(): Unit { - if (size > 1) - sort { a: T, b: T -> a.compareTo(b) } -} - -/** - * Sorts the array in-place. - */ -public fun LongArray.sort(): Unit { - if (size > 1) - sort { a: Long, b: Long -> a.compareTo(b) } -} - /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ @@ -13448,11 +13456,3 @@ public inline fun CharArray.sort(noinline comparison: (Char, Char) -> Int): Unit asDynamic().sort(comparison) } -/** - * Sorts the array in-place according to the order specified by the given [comparator] object. - */ -public fun Array.sortWith(comparator: Comparator): Unit { - if (size > 1) - sort { a, b -> comparator.compare(a, b) } -} - diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index fde80c0eb01..bf523b4a7ef 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -12989,6 +12989,70 @@ public fun CharArray.asList(): List { } } +/** + * Sorts the array in-place. + */ +public fun IntArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun LongArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun ByteArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun ShortArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun DoubleArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun FloatArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun CharArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place according to the natural order of its elements. + */ +@kotlin.internal.InlineOnly +public inline fun > Array.sort(): Unit { + (this as Array).sort() +} + +/** + * Sorts the array in-place according to the order specified by the given [comparator]. + */ +public fun Array.sortWith(comparator: Comparator): Unit { + if (size > 1) java.util.Arrays.sort(this, comparator) +} + /** * Returns a *typed* object array containing all of the elements of this primitive array. */ @@ -13870,69 +13934,15 @@ public inline fun Array.plusElement(element: T): Array { } /** - * Sorts the array in-place. + * Sorts the array in-place according to the natural order of its elements. + * + * @throws ClassCastException if any element of the array is not [Comparable]. */ @kotlin.jvm.JvmVersion public fun Array.sort(): Unit { if (size > 1) java.util.Arrays.sort(this) } -/** - * Sorts the array in-place. - */ -@kotlin.jvm.JvmVersion -public fun ByteArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -@kotlin.jvm.JvmVersion -public fun ShortArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -@kotlin.jvm.JvmVersion -public fun IntArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -@kotlin.jvm.JvmVersion -public fun LongArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -@kotlin.jvm.JvmVersion -public fun FloatArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -@kotlin.jvm.JvmVersion -public fun DoubleArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -@kotlin.jvm.JvmVersion -public fun CharArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - /** * Sorts a range in the array in-place. */ @@ -13997,14 +14007,6 @@ public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { java.util.Arrays.sort(this, fromIndex, toIndex) } -/** - * Sorts the array in-place with the given [comparator]. - */ -@kotlin.jvm.JvmVersion -public fun Array.sortWith(comparator: Comparator): Unit { - if (size > 1) java.util.Arrays.sort(this, comparator) -} - /** * Sorts a range in the array in-place with the given [comparator]. */ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt index 236c6a97a7b..d4b2d9cae3a 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt @@ -144,43 +144,5 @@ fun specialJS(): List { body { "asDynamic().sort(comparison)" } } - templates add f("sortWith(comparator: Comparator)") { - only(ArraysOfObjects) - exclude(PrimitiveType.Boolean) - returns("Unit") - 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()") { - only(ArraysOfPrimitives) - only(numericPrimitives + PrimitiveType.Char) - exclude(PrimitiveType.Long) - returns("Unit") - doc { "Sorts the array in-place." } - annotations("""@library("primitiveArraySort")""") - body { "noImpl" } - } - - templates add f("sort()") { - only(ArraysOfObjects, ArraysOfPrimitives) - only(PrimitiveType.Long) - typeParam("T: Comparable") - returns("Unit") - doc { "Sorts the array in-place." } - body { - """ - if (size > 1) - sort { a: T, b: T -> a.compareTo(b) } - """ - } - } - - return templates } \ No newline at end of file diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index 8bb3081ff48..ef303c1977e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -172,9 +172,15 @@ fun specialJVM(): List { templates add f("sort()") { - only(ArraysOfObjects, ArraysOfPrimitives) - exclude(PrimitiveType.Boolean) - doc { "Sorts the array in-place." } + // left with more generic signature for JVM only + only(ArraysOfObjects) + doc { + """ + Sorts the array in-place according to the natural order of its elements. + + @throws ClassCastException if any element of the array is not [Comparable]. + """ + } returns("Unit") body { "if (size > 1) java.util.Arrays.sort(this)" @@ -191,15 +197,6 @@ fun specialJVM(): List { } } - templates add f("sortWith(comparator: Comparator)") { - only(ArraysOfObjects) - doc { "Sorts the array in-place with the given [comparator]." } - returns("Unit") - body { - "if (size > 1) java.util.Arrays.sort(this, comparator)" - } - } - templates add f("sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size)") { only(ArraysOfObjects) doc { "Sorts a range in the array in-place with the given [comparator]." } @@ -253,6 +250,68 @@ fun specialJVM(): List { } object CommonArrays { + fun f_sortPrimitives() = + (PrimitiveType.numericPrimitives + PrimitiveType.Char).map { primitive -> + f("sort()") { + only(ArraysOfPrimitives) + only(primitive) + doc { "Sorts the array in-place." } + returns("Unit") + body(Platform.JVM) { + "if (size > 1) java.util.Arrays.sort(this)" + } + if (primitive != PrimitiveType.Long) { + annotations(Platform.JS, """@library("primitiveArraySort")""") + body(Platform.JS) { "noImpl" } + } + else { + body(Platform.JS) { + """ + if (size > 1) + sort { a: T, b: T -> a.compareTo(b) } + """ + } + } + } + } + + fun f_sort() = f("sort()") { + only(ArraysOfObjects) + typeParam("T: Comparable") + doc { + """ + Sorts the array in-place according to the natural order of its elements. + """ + } + returns("Unit") + inline(Platform.JVM, Inline.Only) + body(Platform.JVM) { + "(this as Array).sort()" + } + body(Platform.JS) { + """ + if (size > 1) + sort { a: T, b: T -> a.compareTo(b) } + """ + } + } + + fun f_sortWith() = f("sortWith(comparator: Comparator)") { + only(ArraysOfObjects) + buildFamilyPrimitives(Platform.JS, buildFamilyPrimitives.default!! - PrimitiveType.Boolean) + doc { "Sorts the array in-place according to the order specified by the given [comparator]." } + returns("Unit") + body(Platform.JVM) { + "if (size > 1) java.util.Arrays.sort(this, comparator)" + } + body(Platform.JS) { + """ + if (size > 1) + sort { a, b -> comparator.compare(a, b) } + """ + } + } + fun f_asList() = f("asList()") { only(ArraysOfObjects, ArraysOfPrimitives) doc { "Returns a [List] that wraps the original array." } @@ -313,5 +372,5 @@ object CommonArrays { } // TODO: use reflection later to get all functions of matching type - fun templates() = listOf(this.f_asList(), this.f_toTypedArray()) + fun templates() = f_sortPrimitives() + listOf(f_sort(), f_sortWith(), f_asList(), f_toTypedArray()) } \ No newline at end of file