diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 32e42d4e224..990298e352a 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -6162,6 +6162,8 @@ public fun Array.sortedArrayWith(comparator: Comparator): Array * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. * * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > Array.sortedBy(crossinline selector: (T) -> R?): List { return sortedWith(compareBy(selector)) @@ -6169,6 +6171,8 @@ public inline fun > Array.sortedBy(crossinline selec /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > ByteArray.sortedBy(crossinline selector: (Byte) -> R?): List { return sortedWith(compareBy(selector)) @@ -6176,6 +6180,8 @@ public inline fun > ByteArray.sortedBy(crossinline selector: ( /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > ShortArray.sortedBy(crossinline selector: (Short) -> R?): List { return sortedWith(compareBy(selector)) @@ -6183,6 +6189,8 @@ public inline fun > ShortArray.sortedBy(crossinline selector: /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > IntArray.sortedBy(crossinline selector: (Int) -> R?): List { return sortedWith(compareBy(selector)) @@ -6190,6 +6198,8 @@ public inline fun > IntArray.sortedBy(crossinline selector: (I /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > LongArray.sortedBy(crossinline selector: (Long) -> R?): List { return sortedWith(compareBy(selector)) @@ -6197,6 +6207,8 @@ public inline fun > LongArray.sortedBy(crossinline selector: ( /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > FloatArray.sortedBy(crossinline selector: (Float) -> R?): List { return sortedWith(compareBy(selector)) @@ -6204,6 +6216,8 @@ public inline fun > FloatArray.sortedBy(crossinline selector: /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > DoubleArray.sortedBy(crossinline selector: (Double) -> R?): List { return sortedWith(compareBy(selector)) @@ -6211,6 +6225,8 @@ public inline fun > DoubleArray.sortedBy(crossinline selector: /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > BooleanArray.sortedBy(crossinline selector: (Boolean) -> R?): List { return sortedWith(compareBy(selector)) @@ -6218,6 +6234,8 @@ public inline fun > BooleanArray.sortedBy(crossinline selector /** * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > CharArray.sortedBy(crossinline selector: (Char) -> R?): List { return sortedWith(compareBy(selector)) diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index e8fb686ebfc..c2498309394 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -964,6 +964,8 @@ public fun > Iterable.sorted(): List { * Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. * * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List { return sortedWith(compareBy(selector)) diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 5f3b32e3687..5c627b1f13e 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -533,6 +533,8 @@ public fun > Sequence.sorted(): Sequence { * The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting. * * The operation is _intermediate_ and _stateful_. + * + * @sample samples.collections.Collections.Sorting.sortedBy */ public inline fun > Sequence.sortedBy(crossinline selector: (T) -> R?): Sequence { return sortedWith(compareBy(selector)) diff --git a/libraries/stdlib/samples/test/samples/collections/collections.kt b/libraries/stdlib/samples/test/samples/collections/collections.kt index 9b33e17732f..84745776464 100644 --- a/libraries/stdlib/samples/test/samples/collections/collections.kt +++ b/libraries/stdlib/samples/test/samples/collections/collections.kt @@ -809,6 +809,14 @@ class Collections { assertPrints(people.joinToString(), "Sweyn Forkbeard, Ragnar Lodbrok, Bjorn Ironside") } + @Sample + fun sortedBy() { + val list = listOf("aaa", "cc", "bbbb") + val sorted = list.sortedBy { it.length } + + assertPrints(list, "[aaa, cc, bbbb]") + assertPrints(sorted, "[cc, aaa, bbbb]") + } } class Filtering { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index 4f719f88537..bd78ab39752 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -432,7 +432,6 @@ object Ordering : TemplateGroupBase() { if (f != ArraysOfPrimitives) { appendStableSortNote() } - specialFor(Sequences) { returns("SELF") doc { @@ -441,6 +440,8 @@ object Ordering : TemplateGroupBase() { appendStableSortNote() sequenceClassification(intermediate, stateful) } + sample("samples.collections.Collections.Sorting.sortedBy") + body { "return sortedWith(compareBy(selector))" }