diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index c78bd1ca5d82d..4bc0ec13d8d20 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -6736,36 +6736,50 @@ public expect fun Array.plusElement(element: T): Array /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public expect fun IntArray.sort(): Unit /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public expect fun LongArray.sort(): Unit /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public expect fun ByteArray.sort(): Unit /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public expect fun ShortArray.sort(): Unit /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public expect fun DoubleArray.sort(): Unit /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public expect fun FloatArray.sort(): Unit /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public expect fun CharArray.sort(): Unit @@ -6773,6 +6787,8 @@ public expect fun CharArray.sort(): Unit * Sorts the array in-place according to the natural order of its elements. * * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + * + * @sample samples.collections.Arrays.Sorting.sortArrayOfComparable */ public expect fun > Array.sort(): Unit diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 758eb6a666048..e12c98456b5e9 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -3424,6 +3424,8 @@ public inline operator fun UShortArray.plus(elements: UShortArray): UShortArray /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @SinceKotlin("1.3") @ExperimentalUnsignedTypes @@ -3433,6 +3435,8 @@ public fun UIntArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @SinceKotlin("1.3") @ExperimentalUnsignedTypes @@ -3442,6 +3446,8 @@ public fun ULongArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @SinceKotlin("1.3") @ExperimentalUnsignedTypes @@ -3451,6 +3457,8 @@ public fun UByteArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @SinceKotlin("1.3") @ExperimentalUnsignedTypes diff --git a/libraries/stdlib/js-ir/src/generated/_ArraysJs.kt b/libraries/stdlib/js-ir/src/generated/_ArraysJs.kt index 64d3b49688ffe..77810754cffb9 100644 --- a/libraries/stdlib/js-ir/src/generated/_ArraysJs.kt +++ b/libraries/stdlib/js-ir/src/generated/_ArraysJs.kt @@ -1259,6 +1259,8 @@ public actual inline fun Array.plusElement(element: T): Array { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun IntArray.sort(): Unit { this.asDynamic().sort() @@ -1266,6 +1268,8 @@ public actual fun IntArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun LongArray.sort(): Unit { if (size > 1) sort { a: Long, b: Long -> a.compareTo(b) } @@ -1273,6 +1277,8 @@ public actual fun LongArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun ByteArray.sort(): Unit { this.asDynamic().sort() @@ -1280,6 +1286,8 @@ public actual fun ByteArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun ShortArray.sort(): Unit { this.asDynamic().sort() @@ -1287,6 +1295,8 @@ public actual fun ShortArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun DoubleArray.sort(): Unit { this.asDynamic().sort() @@ -1294,6 +1304,8 @@ public actual fun DoubleArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun FloatArray.sort(): Unit { this.asDynamic().sort() @@ -1301,6 +1313,8 @@ public actual fun FloatArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun CharArray.sort(): Unit { this.asDynamic().sort(::primitiveCompareTo) @@ -1310,6 +1324,8 @@ public actual fun CharArray.sort(): Unit { * Sorts the array in-place according to the natural order of its elements. * * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + * + * @sample samples.collections.Arrays.Sorting.sortArrayOfComparable */ public actual fun > Array.sort(): Unit { if (size > 1) sortArray(this) diff --git a/libraries/stdlib/js/src/generated/_ArraysJs.kt b/libraries/stdlib/js/src/generated/_ArraysJs.kt index 2b18f2200942c..0c7fd962f3e15 100644 --- a/libraries/stdlib/js/src/generated/_ArraysJs.kt +++ b/libraries/stdlib/js/src/generated/_ArraysJs.kt @@ -1271,6 +1271,8 @@ public actual inline fun Array.plusElement(element: T): Array { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @library("primitiveArraySort") public actual fun IntArray.sort(): Unit { @@ -1279,6 +1281,8 @@ public actual fun IntArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun LongArray.sort(): Unit { if (size > 1) sort { a: Long, b: Long -> a.compareTo(b) } @@ -1286,6 +1290,8 @@ public actual fun LongArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @library("primitiveArraySort") public actual fun ByteArray.sort(): Unit { @@ -1294,6 +1300,8 @@ public actual fun ByteArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @library("primitiveArraySort") public actual fun ShortArray.sort(): Unit { @@ -1302,6 +1310,8 @@ public actual fun ShortArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @library("primitiveArraySort") public actual fun DoubleArray.sort(): Unit { @@ -1310,6 +1320,8 @@ public actual fun DoubleArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @library("primitiveArraySort") public actual fun FloatArray.sort(): Unit { @@ -1318,6 +1330,8 @@ public actual fun FloatArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ @library("primitiveArraySort") public actual fun CharArray.sort(): Unit { @@ -1328,6 +1342,8 @@ public actual fun CharArray.sort(): Unit { * Sorts the array in-place according to the natural order of its elements. * * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + * + * @sample samples.collections.Arrays.Sorting.sortArrayOfComparable */ public actual fun > Array.sort(): Unit { if (size > 1) sortArray(this) diff --git a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt index 4f3e079574ce4..5efee83ac136b 100644 --- a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt @@ -1728,6 +1728,8 @@ public actual inline fun Array.plusElement(element: T): Array { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun IntArray.sort(): Unit { if (size > 1) java.util.Arrays.sort(this) @@ -1735,6 +1737,8 @@ public actual fun IntArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun LongArray.sort(): Unit { if (size > 1) java.util.Arrays.sort(this) @@ -1742,6 +1746,8 @@ public actual fun LongArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun ByteArray.sort(): Unit { if (size > 1) java.util.Arrays.sort(this) @@ -1749,6 +1755,8 @@ public actual fun ByteArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun ShortArray.sort(): Unit { if (size > 1) java.util.Arrays.sort(this) @@ -1756,6 +1764,8 @@ public actual fun ShortArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun DoubleArray.sort(): Unit { if (size > 1) java.util.Arrays.sort(this) @@ -1763,6 +1773,8 @@ public actual fun DoubleArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun FloatArray.sort(): Unit { if (size > 1) java.util.Arrays.sort(this) @@ -1770,6 +1782,8 @@ public actual fun FloatArray.sort(): Unit { /** * Sorts the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortArray */ public actual fun CharArray.sort(): Unit { if (size > 1) java.util.Arrays.sort(this) @@ -1779,6 +1793,8 @@ public actual fun CharArray.sort(): Unit { * Sorts the array in-place according to the natural order of its elements. * * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + * + * @sample samples.collections.Arrays.Sorting.sortArrayOfComparable */ @kotlin.internal.InlineOnly public actual inline fun > Array.sort(): Unit { @@ -1801,6 +1817,8 @@ public fun Array.sort(): Unit { * Sorts a range in the array in-place. * * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfComparable */ public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { java.util.Arrays.sort(this, fromIndex, toIndex) @@ -1808,6 +1826,8 @@ public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit /** * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfComparable */ public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { java.util.Arrays.sort(this, fromIndex, toIndex) @@ -1815,6 +1835,8 @@ public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { /** * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfComparable */ public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { java.util.Arrays.sort(this, fromIndex, toIndex) @@ -1822,6 +1844,8 @@ public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { /** * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfComparable */ public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { java.util.Arrays.sort(this, fromIndex, toIndex) @@ -1829,6 +1853,8 @@ public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { /** * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfComparable */ public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { java.util.Arrays.sort(this, fromIndex, toIndex) @@ -1836,6 +1862,8 @@ public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { /** * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfComparable */ public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { java.util.Arrays.sort(this, fromIndex, toIndex) @@ -1843,6 +1871,8 @@ public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { /** * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfComparable */ public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { java.util.Arrays.sort(this, fromIndex, toIndex) @@ -1850,6 +1880,8 @@ public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { /** * Sorts a range in the array in-place. + * + * @sample samples.collections.Arrays.Sorting.sortRangeOfComparable */ public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { java.util.Arrays.sort(this, fromIndex, toIndex) diff --git a/libraries/stdlib/jvm/src/kotlin/collections/MutableCollectionsJVM.kt b/libraries/stdlib/jvm/src/kotlin/collections/MutableCollectionsJVM.kt index 7d320b086adb2..d3c53e783f000 100644 --- a/libraries/stdlib/jvm/src/kotlin/collections/MutableCollectionsJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/collections/MutableCollectionsJVM.kt @@ -24,6 +24,7 @@ public inline fun MutableList.sort(comparison: (T, T) -> Int): Unit = thr * Sorts elements in the list in-place according to their natural sort order. * * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + * @sample samples.collections.Collections.Sorting.sortMutableList */ public actual fun > MutableList.sort(): Unit { if (size > 1) java.util.Collections.sort(this) diff --git a/libraries/stdlib/samples/test/samples/collections/arrays.kt b/libraries/stdlib/samples/test/samples/collections/arrays.kt index 2a6a5152d91f4..e609aca7ac4b4 100644 --- a/libraries/stdlib/samples/test/samples/collections/arrays.kt +++ b/libraries/stdlib/samples/test/samples/collections/arrays.kt @@ -131,4 +131,53 @@ class Arrays { } } + class Sorting { + + @Sample + fun sortArray() { + val intArray = intArrayOf(4, 3, 2, 1) + + assertPrints(intArray.joinToString(), "4, 3, 2, 1") + + intArray.sort() + + assertPrints(intArray.joinToString(), "1, 2, 3, 4") + } + + @Sample + fun sortArrayOfComparable() { + data class Person(val firstName: String, val lastName: String) : Comparable { + override fun compareTo(other: Person): Int { + return lastName.compareTo(other.lastName) + } + + } + + val people = arrayOf( + Person("Ragnar", "Lodbrok"), + Person("Bjorn", "Ironside"), + Person("Sweyn", "Forkbeard") + ) + + assertPrints(people.joinToString(), "Person(firstName=Ragnar, lastName=Lodbrok), Person(firstName=Bjorn, lastName=Ironside), Person(firstName=Sweyn, lastName=Forkbeard)") + + people.sort() + + assertPrints(people.joinToString(), "Person(firstName=Sweyn, lastName=Forkbeard), Person(firstName=Bjorn, lastName=Ironside), Person(firstName=Ragnar, lastName=Lodbrok)") + + } + + @Sample + fun sortRangeOfArray() { + val intArray = intArrayOf(4, 3, 2, 1) + + assertPrints(intArray.joinToString(), "4, 3, 2, 1") + + intArray.sort(0, 3) + + assertPrints(intArray.joinToString(), "2, 3, 4, 1") + } + + } + } \ No newline at end of file diff --git a/libraries/stdlib/samples/test/samples/collections/collections.kt b/libraries/stdlib/samples/test/samples/collections/collections.kt index 1de750ddf8d12..32a3dd8e5a06d 100644 --- a/libraries/stdlib/samples/test/samples/collections/collections.kt +++ b/libraries/stdlib/samples/test/samples/collections/collections.kt @@ -561,4 +561,41 @@ class Collections { assertPrints(emptyList.elementAtOrElse(0) { "no int" }, "no int") } } + + class Sorting { + + @Sample + fun sortMutableList() { + val mutableList = mutableListOf(4, 3, 2, 1) + + assertPrints(mutableList.joinToString(), "4, 3, 2, 1") + + mutableList.sort() + + assertPrints(mutableList.joinToString(), "1, 2, 3, 4") + } + + @Sample + fun sortRangeOfComparable() { + data class Person(val firstName: String, val lastName: String) : Comparable { + override fun compareTo(other: Person): Int { + return lastName.compareTo(other.lastName) + } + + } + + val people = arrayOf( + Person("Ragnar", "Lodbrok"), + Person("Bjorn", "Ironside"), + Person("Sweyn", "Forkbeard") + ) + + assertPrints(people.joinToString(), "Person(firstName=Ragnar, lastName=Lodbrok), Person(firstName=Bjorn, lastName=Ironside), Person(firstName=Sweyn, lastName=Forkbeard)") + + people.sort(0, 2) + + assertPrints(people.joinToString(), "Person(firstName=Bjorn, lastName=Ironside), Person(firstName=Ragnar, lastName=Lodbrok), Person(firstName=Sweyn, lastName=Forkbeard)") + } + + } } \ No newline at end of file diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 9e19f25472876..3fe9372075f0b 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -1016,8 +1016,12 @@ object ArrayOps : TemplateGroupBase() { typeParam("T : Comparable") doc { "Sorts the array in-place according to the natural order of its elements." } appendStableSortNote() + specialFor(ArraysOfObjects) { + sample("samples.collections.Arrays.Sorting.sortArrayOfComparable") + } specialFor(ArraysOfPrimitives, ArraysOfUnsigned) { doc { "Sorts the array in-place." } + sample("samples.collections.Arrays.Sorting.sortArray") } returns("Unit") @@ -1143,6 +1147,7 @@ object ArrayOps : TemplateGroupBase() { specialFor(ArraysOfObjects) { appendStableSortNote() } + sample("samples.collections.Arrays.Sorting.sortRangeOfComparable") returns("Unit") body { "java.util.Arrays.sort(this, fromIndex, toIndex)"