KT-16602: Provide samples for sorting API usage

This commit is contained in:
Dat Trieu
2019-07-12 20:32:03 +02:00
committed by ilya-g
parent 3f5d64e635
commit 38d26b1d92
9 changed files with 180 additions and 0 deletions
@@ -6736,36 +6736,50 @@ public expect fun <T> Array<T>.plusElement(element: T): Array<T>
/**
* 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 <T : Comparable<T>> Array<out T>.sort(): Unit
@@ -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
@@ -1259,6 +1259,8 @@ public actual inline fun <T> Array<out T>.plusElement(element: T): Array<T> {
/**
* 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 <T : Comparable<T>> Array<out T>.sort(): Unit {
if (size > 1) sortArray(this)
@@ -1271,6 +1271,8 @@ public actual inline fun <T> Array<out T>.plusElement(element: T): Array<T> {
/**
* 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 <T : Comparable<T>> Array<out T>.sort(): Unit {
if (size > 1) sortArray(this)
@@ -1728,6 +1728,8 @@ public actual inline fun <T> Array<T>.plusElement(element: T): Array<T> {
/**
* 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 <T : Comparable<T>> Array<out T>.sort(): Unit {
@@ -1801,6 +1817,8 @@ public fun <T> Array<out T>.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 <T> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
java.util.Arrays.sort(this, fromIndex, toIndex)
@@ -1808,6 +1826,8 @@ public fun <T> Array<out T>.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)
@@ -24,6 +24,7 @@ public inline fun <T> MutableList<T>.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 <T : Comparable<T>> MutableList<T>.sort(): Unit {
if (size > 1) java.util.Collections.sort(this)
@@ -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<Person> {
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")
}
}
}
@@ -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<Person> {
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)")
}
}
}
@@ -1016,8 +1016,12 @@ object ArrayOps : TemplateGroupBase() {
typeParam("T : Comparable<T>")
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)"