In-place sorting.
#KT-9034
This commit is contained in:
@@ -4477,76 +4477,151 @@ public fun ShortArray.reversedArray(): ShortArray {
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place according to natural sort order of the value returned by specified [selector] function.
|
||||
*/
|
||||
public inline fun <T, R : Comparable<R>> Array<out T>.sortBy(crossinline selector: (T) -> R?): Unit {
|
||||
if (size > 1) sortWith(compareBy(selector))
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place descending according to natural sort order of the value returned by specified [selector] function.
|
||||
*/
|
||||
public inline fun <T, R : Comparable<R>> Array<out T>.sortByDescending(crossinline selector: (T) -> R?): Unit {
|
||||
if (size > 1) sortWith(compareByDescending(selector))
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place descending according to their natural sort order.
|
||||
*/
|
||||
public fun <T : Comparable<T>> Array<out T>.sortDescending(): Unit {
|
||||
sortWith(reverseOrder())
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place descending according to their natural sort order.
|
||||
*/
|
||||
public fun ByteArray.sortDescending(): Unit {
|
||||
if (size > 1) {
|
||||
sort()
|
||||
reverse()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place descending according to their natural sort order.
|
||||
*/
|
||||
public fun CharArray.sortDescending(): Unit {
|
||||
if (size > 1) {
|
||||
sort()
|
||||
reverse()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place descending according to their natural sort order.
|
||||
*/
|
||||
public fun DoubleArray.sortDescending(): Unit {
|
||||
if (size > 1) {
|
||||
sort()
|
||||
reverse()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place descending according to their natural sort order.
|
||||
*/
|
||||
public fun FloatArray.sortDescending(): Unit {
|
||||
if (size > 1) {
|
||||
sort()
|
||||
reverse()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place descending according to their natural sort order.
|
||||
*/
|
||||
public fun IntArray.sortDescending(): Unit {
|
||||
if (size > 1) {
|
||||
sort()
|
||||
reverse()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place descending according to their natural sort order.
|
||||
*/
|
||||
public fun LongArray.sortDescending(): Unit {
|
||||
if (size > 1) {
|
||||
sort()
|
||||
reverse()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the array in-place descending according to their natural sort order.
|
||||
*/
|
||||
public fun ShortArray.sortDescending(): Unit {
|
||||
if (size > 1) {
|
||||
sort()
|
||||
reverse()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to their natural sort order.
|
||||
*/
|
||||
public fun <T : Comparable<T>> Array<out T>.sorted(): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to their natural sort order.
|
||||
*/
|
||||
public fun ByteArray.sorted(): List<Byte> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to their natural sort order.
|
||||
*/
|
||||
public fun CharArray.sorted(): List<Char> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to their natural sort order.
|
||||
*/
|
||||
public fun DoubleArray.sorted(): List<Double> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to their natural sort order.
|
||||
*/
|
||||
public fun FloatArray.sorted(): List<Float> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to their natural sort order.
|
||||
*/
|
||||
public fun IntArray.sorted(): List<Int> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to their natural sort order.
|
||||
*/
|
||||
public fun LongArray.sorted(): List<Long> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to their natural sort order.
|
||||
*/
|
||||
public fun ShortArray.sorted(): List<Short> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4618,8 +4693,7 @@ public fun ShortArray.sortedArray(): ShortArray {
|
||||
*/
|
||||
public fun <T : Comparable<T>> Array<out T>.sortedArrayDescending(): Array<out T> {
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use reverseOrder<T>()
|
||||
return this.copyOf().apply { sortWith(comparator { a, b -> b.compareTo(a) }) }
|
||||
return this.copyOf().apply { sortWith(reverseOrder()) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4627,8 +4701,7 @@ public fun <T : Comparable<T>> Array<out T>.sortedArrayDescending(): Array<out T
|
||||
*/
|
||||
public fun ByteArray.sortedArrayDescending(): ByteArray {
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use in-place reverse
|
||||
return this.copyOf().apply { sort() }.reversedArray()
|
||||
return this.copyOf().apply { sortDescending() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4636,8 +4709,7 @@ public fun ByteArray.sortedArrayDescending(): ByteArray {
|
||||
*/
|
||||
public fun CharArray.sortedArrayDescending(): CharArray {
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use in-place reverse
|
||||
return this.copyOf().apply { sort() }.reversedArray()
|
||||
return this.copyOf().apply { sortDescending() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4645,8 +4717,7 @@ public fun CharArray.sortedArrayDescending(): CharArray {
|
||||
*/
|
||||
public fun DoubleArray.sortedArrayDescending(): DoubleArray {
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use in-place reverse
|
||||
return this.copyOf().apply { sort() }.reversedArray()
|
||||
return this.copyOf().apply { sortDescending() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4654,8 +4725,7 @@ public fun DoubleArray.sortedArrayDescending(): DoubleArray {
|
||||
*/
|
||||
public fun FloatArray.sortedArrayDescending(): FloatArray {
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use in-place reverse
|
||||
return this.copyOf().apply { sort() }.reversedArray()
|
||||
return this.copyOf().apply { sortDescending() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4663,8 +4733,7 @@ public fun FloatArray.sortedArrayDescending(): FloatArray {
|
||||
*/
|
||||
public fun IntArray.sortedArrayDescending(): IntArray {
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use in-place reverse
|
||||
return this.copyOf().apply { sort() }.reversedArray()
|
||||
return this.copyOf().apply { sortDescending() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4672,8 +4741,7 @@ public fun IntArray.sortedArrayDescending(): IntArray {
|
||||
*/
|
||||
public fun LongArray.sortedArrayDescending(): LongArray {
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use in-place reverse
|
||||
return this.copyOf().apply { sort() }.reversedArray()
|
||||
return this.copyOf().apply { sortDescending() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4681,8 +4749,7 @@ public fun LongArray.sortedArrayDescending(): LongArray {
|
||||
*/
|
||||
public fun ShortArray.sortedArrayDescending(): ShortArray {
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use in-place reverse
|
||||
return this.copyOf().apply { sort() }.reversedArray()
|
||||
return this.copyOf().apply { sortDescending() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4823,7 +4890,7 @@ public inline fun <R : Comparable<R>> ShortArray.sortedByDescending(crossinline
|
||||
* Returns a list of all elements sorted descending according to their natural sort order.
|
||||
*/
|
||||
public fun <T : Comparable<T>> Array<out T>.sortedDescending(): List<T> {
|
||||
return sortedWith(comparator { x, y -> y.compareTo(x) })
|
||||
return sortedWith(reverseOrder())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4879,81 +4946,63 @@ public fun ShortArray.sortedDescending(): List<Short> {
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun <T> Array<out T>.sortedWith(comparator: Comparator<in T>): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun BooleanArray.sortedWith(comparator: Comparator<in Boolean>): List<Boolean> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun ByteArray.sortedWith(comparator: Comparator<in Byte>): List<Byte> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun CharArray.sortedWith(comparator: Comparator<in Char>): List<Char> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun DoubleArray.sortedWith(comparator: Comparator<in Double>): List<Double> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun FloatArray.sortedWith(comparator: Comparator<in Float>): List<Float> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun IntArray.sortedWith(comparator: Comparator<in Int>): List<Int> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun LongArray.sortedWith(comparator: Comparator<in Long>): List<Long> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun ShortArray.sortedWith(comparator: Comparator<in Short>): List<Short> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -806,13 +806,32 @@ public fun <T> Iterable<T>.reversed(): List<T> {
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the collection in-place according to natural sort order of the value returned by specified [selector] function.
|
||||
*/
|
||||
public inline fun <T, R : Comparable<R>> MutableList<T>.sortBy(crossinline selector: (T) -> R?): Unit {
|
||||
if (size > 1) sortWith(compareBy(selector))
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the collection in-place descending according to natural sort order of the value returned by specified [selector] function.
|
||||
*/
|
||||
public inline fun <T, R : Comparable<R>> MutableList<T>.sortByDescending(crossinline selector: (T) -> R?): Unit {
|
||||
if (size > 1) sortWith(compareByDescending(selector))
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the collection in-place descending according to their natural sort order.
|
||||
*/
|
||||
public fun <T : Comparable<T>> MutableList<T>.sortDescending(): Unit {
|
||||
sortWith(reverseOrder())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to their natural sort order.
|
||||
*/
|
||||
public fun <T : Comparable<T>> Iterable<T>.sorted(): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -833,16 +852,14 @@ public inline fun <T, R : Comparable<R>> Iterable<T>.sortedByDescending(crossinl
|
||||
* Returns a list of all elements sorted descending according to their natural sort order.
|
||||
*/
|
||||
public fun <T : Comparable<T>> Iterable<T>.sortedDescending(): List<T> {
|
||||
return sortedWith(comparator { x, y -> y.compareTo(x) })
|
||||
return sortedWith(reverseOrder())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all elements sorted according to the specified [comparator].
|
||||
*/
|
||||
public fun <T> Iterable<T>.sortedWith(comparator: Comparator<in T>): List<T> {
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -422,7 +422,7 @@ public fun <T : Comparable<T>> Sequence<T>.sorted(): Sequence<T> {
|
||||
return object : Sequence<T> {
|
||||
override fun iterator(): Iterator<T> {
|
||||
val sortedList = this@sorted.toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
sortedList.sort()
|
||||
return sortedList.iterator()
|
||||
}
|
||||
}
|
||||
@@ -446,7 +446,7 @@ public inline fun <T, R : Comparable<R>> Sequence<T>.sortedByDescending(crossinl
|
||||
* Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order.
|
||||
*/
|
||||
public fun <T : Comparable<T>> Sequence<T>.sortedDescending(): Sequence<T> {
|
||||
return sortedWith(comparator { x, y -> y.compareTo(x) })
|
||||
return sortedWith(reverseOrder())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -456,7 +456,7 @@ public fun <T> Sequence<T>.sortedWith(comparator: Comparator<in T>): Sequence<T>
|
||||
return object : Sequence<T> {
|
||||
override fun iterator(): Iterator<T> {
|
||||
val sortedList = this@sortedWith.toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
sortedList.sortWith(comparator)
|
||||
return sortedList.iterator()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Checks if all elements in the specified collection are contained in this collection.
|
||||
*
|
||||
@@ -247,3 +249,17 @@ public fun <T> MutableCollection<in T>.retainAll(sequence: Sequence<T>) {
|
||||
else
|
||||
clear()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the list in-place according to their natural sort order.
|
||||
* */
|
||||
public fun <T: Comparable<T>> MutableList<T>.sort(): Unit {
|
||||
if (size > 1) java.util.Collections.sort(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the list in-place according to order specified with [comparator].
|
||||
*/
|
||||
public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
|
||||
if (size > 1) java.util.Collections.sort(this, comparator)
|
||||
}
|
||||
|
||||
@@ -823,25 +823,34 @@ class ArraysTest {
|
||||
val intArr = intArrayOf(5, 2, 1, 9, 80, Int.MIN_VALUE, Int.MAX_VALUE)
|
||||
intArr.sort()
|
||||
assertArrayNotSameButEquals(intArrayOf(Int.MIN_VALUE, 1, 2, 5, 9, 80, Int.MAX_VALUE), intArr)
|
||||
intArr.sortDescending()
|
||||
assertArrayNotSameButEquals(intArrayOf(Int.MAX_VALUE, 80, 9, 5, 2, 1, Int.MIN_VALUE), intArr)
|
||||
|
||||
val longArr = longArrayOf(200, 2, 1, 4, 3, Long.MIN_VALUE, Long.MAX_VALUE)
|
||||
longArr.sort()
|
||||
assertArrayNotSameButEquals(longArrayOf(Long.MIN_VALUE, 1, 2, 3, 4, 200, Long.MAX_VALUE), longArr)
|
||||
longArr.sortDescending()
|
||||
assertArrayNotSameButEquals(longArrayOf(Long.MAX_VALUE, 200, 4, 3, 2, 1, Long.MIN_VALUE), longArr)
|
||||
|
||||
val charArr = charArrayOf('d', 'c', 'E', 'a', '\u0000', '\uFFFF')
|
||||
charArr.sort()
|
||||
assertArrayNotSameButEquals(charArrayOf('\u0000', 'E', 'a', 'c', 'd', '\uFFFF'), charArr)
|
||||
charArr.sortDescending()
|
||||
assertArrayNotSameButEquals(charArrayOf('\uFFFF', 'd', 'c', 'a', 'E', '\u0000'), charArr)
|
||||
|
||||
|
||||
val strArr = arrayOf("9", "80", "all", "Foo")
|
||||
strArr.sort()
|
||||
assertArrayNotSameButEquals(arrayOf("80", "9", "Foo", "all"), strArr)
|
||||
strArr.sortDescending()
|
||||
assertArrayNotSameButEquals(arrayOf("all", "Foo", "9", "80"), strArr)
|
||||
}
|
||||
|
||||
@test fun sorted() {
|
||||
assertTrue(arrayOf<Long>().sorted().none())
|
||||
assertEquals(listOf(1), arrayOf(1).sorted())
|
||||
|
||||
fun arrayData<A, T: Comparable<T>>(vararg values: T, toArray: Array<out T>.() -> A) = ArraySortedChecker<A, T>(values.toArray(), comparator { a, b -> a.compareTo(b) })
|
||||
fun arrayData<A, T: Comparable<T>>(vararg values: T, toArray: Array<out T>.() -> A) = ArraySortedChecker<A, T>(values.toArray(), naturalOrder())
|
||||
|
||||
with (arrayData("ac", "aD", "aba") { toList().toTypedArray() }) {
|
||||
checkSorted<List<String>>({ sorted() }, { sortedDescending() }, { iterator() })
|
||||
@@ -875,6 +884,18 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@test fun sortByInPlace() {
|
||||
val data = arrayOf("aa" to 20, "ab" to 3, "aa" to 3)
|
||||
data.sortBy { it.second }
|
||||
assertArrayNotSameButEquals(arrayOf("ab" to 3, "aa" to 3, "aa" to 20), data)
|
||||
|
||||
data.sortBy { it.first }
|
||||
assertArrayNotSameButEquals(arrayOf("aa" to 3, "aa" to 20, "ab" to 3), data)
|
||||
|
||||
data.sortByDescending { (it.first + it.second).length }
|
||||
assertArrayNotSameButEquals(arrayOf("aa" to 20, "aa" to 3, "ab" to 3), data)
|
||||
}
|
||||
|
||||
@test fun sortedBy() {
|
||||
val values = arrayOf("ac", "aD", "aba")
|
||||
val indices = values.indices.toList().toIntArray()
|
||||
@@ -900,11 +921,16 @@ class ArraysTest {
|
||||
|
||||
arrayData(arrayOf(0, 1, 2, 3, 4, 5), comparator)
|
||||
.checkSorted<Array<out Int>>( { sortedArrayWith(comparator) }, { sortedArrayWith(comparator.reversed()) }, { iterator() })
|
||||
|
||||
// in-place
|
||||
val array = Array(6) { it }
|
||||
array.sortWith(comparator)
|
||||
array.iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }
|
||||
}
|
||||
}
|
||||
|
||||
private class ArraySortedChecker<A, T>(val array: A, val comparator: Comparator<in T>) {
|
||||
public fun checkSorted<R>(sorted: A.() -> R, sortedDescending: A.() -> R, iterator: R.() -> Iterator<T>) {
|
||||
public fun <R> checkSorted(sorted: A.() -> R, sortedDescending: A.() -> R, iterator: R.() -> Iterator<T>) {
|
||||
array.sorted().iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }
|
||||
array.sortedDescending().iterator().assertSorted { a, b -> comparator.compare(a, b) >= 0 }
|
||||
}
|
||||
|
||||
@@ -563,16 +563,40 @@ class CollectionTest {
|
||||
expect(listOf(1)) { listOf(1) take 10 }
|
||||
}
|
||||
|
||||
@test fun sortInPlace() {
|
||||
val data = listOf(11, 3, 7)
|
||||
|
||||
val asc = data.toArrayList()
|
||||
asc.sort()
|
||||
assertEquals(listOf(3, 7, 11), asc)
|
||||
|
||||
val desc = data.toArrayList()
|
||||
desc.sortDescending()
|
||||
assertEquals(listOf(11, 7, 3), desc)
|
||||
}
|
||||
|
||||
@test fun sorted() {
|
||||
val data = listOf(11, 3, 7)
|
||||
assertEquals(listOf(3, 7, 11), data.sorted())
|
||||
assertEquals(listOf(11, 7, 3), data.sortedDescending())
|
||||
}
|
||||
|
||||
@test fun sortByInPlace() {
|
||||
val data = arrayListOf("aa" to 20, "ab" to 3, "aa" to 3)
|
||||
data.sortBy { it.second }
|
||||
assertEquals(listOf("ab" to 3, "aa" to 3, "aa" to 20), data)
|
||||
|
||||
data.sortBy { it.first }
|
||||
assertEquals(listOf("aa" to 3, "aa" to 20, "ab" to 3), data)
|
||||
|
||||
data.sortByDescending { (it.first + it.second).length }
|
||||
assertEquals(listOf("aa" to 20, "aa" to 3, "ab" to 3), data)
|
||||
}
|
||||
|
||||
@test fun sortedBy() {
|
||||
assertEquals(listOf("two" to 2, "three" to 3), listOf("three" to 3, "two" to 2).sortedBy { it.second })
|
||||
assertEquals(listOf("three" to 3, "two" to 2), listOf("three" to 3, "two" to 2).sortedBy { it.first })
|
||||
assertEquals(listOf("three", "two"), listOf("two", "three").sortedByDescending { it.length() })
|
||||
assertEquals(listOf("two" to 3, "three" to 20), listOf("three" to 20, "two" to 3).sortedBy { it.second })
|
||||
assertEquals(listOf("three" to 20, "two" to 3), listOf("three" to 20, "two" to 3).sortedBy { it.first })
|
||||
assertEquals(listOf("three", "two"), listOf("two", "three").sortedByDescending { it.length })
|
||||
}
|
||||
|
||||
@test fun sortedNullableBy() {
|
||||
|
||||
@@ -99,9 +99,7 @@ fun ordering(): List<GenericFunction> {
|
||||
typeParam("T : Comparable<T>")
|
||||
body {
|
||||
"""
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
return sortedList
|
||||
return toArrayList().apply { sort() }
|
||||
"""
|
||||
}
|
||||
|
||||
@@ -114,7 +112,7 @@ fun ordering(): List<GenericFunction> {
|
||||
return object : Sequence<T> {
|
||||
override fun iterator(): Iterator<T> {
|
||||
val sortedList = this@sorted.toArrayList()
|
||||
java.util.Collections.sort(sortedList)
|
||||
sortedList.sort()
|
||||
return sortedList.iterator()
|
||||
}
|
||||
}
|
||||
@@ -138,6 +136,25 @@ fun ordering(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("sortDescending()") {
|
||||
only(Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||
exclude(PrimitiveType.Boolean)
|
||||
doc { f -> """Sorts elements in the ${f.collection} in-place descending according to their natural sort order.""" }
|
||||
returns("Unit")
|
||||
typeParam("T : Comparable<T>")
|
||||
customReceiver(Lists) { "MutableList<T>" }
|
||||
|
||||
body { """sortWith(reverseOrder())""" }
|
||||
body(ArraysOfPrimitives) {
|
||||
"""
|
||||
if (size > 1) {
|
||||
sort()
|
||||
reverse()
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("sortedDescending()") {
|
||||
exclude(PrimitiveType.Boolean)
|
||||
|
||||
@@ -150,7 +167,7 @@ fun ordering(): List<GenericFunction> {
|
||||
typeParam("T : Comparable<T>")
|
||||
body {
|
||||
"""
|
||||
return sortedWith(comparator { x, y -> y.compareTo(x) })
|
||||
return sortedWith(reverseOrder())
|
||||
"""
|
||||
}
|
||||
body(ArraysOfPrimitives) {
|
||||
@@ -163,11 +180,6 @@ fun ordering(): List<GenericFunction> {
|
||||
doc(Sequences) {
|
||||
"Returns a sequence that yields elements of this sequence sorted descending according to their natural sort order."
|
||||
}
|
||||
body(Sequences) {
|
||||
"""
|
||||
return sortedWith(comparator { x, y -> y.compareTo(x) })
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("sortedArrayDescending()") {
|
||||
@@ -181,16 +193,14 @@ fun ordering(): List<GenericFunction> {
|
||||
body(ArraysOfObjects) {
|
||||
"""
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use reverseOrder<T>()
|
||||
return this.copyOf().apply { sortWith(comparator { a, b -> b.compareTo(a) }) }
|
||||
return this.copyOf().apply { sortWith(reverseOrder()) }
|
||||
"""
|
||||
|
||||
}
|
||||
body() {
|
||||
"""
|
||||
if (isEmpty()) return this
|
||||
// TODO: Use in-place reverse
|
||||
return this.copyOf().apply { sort() }.reversedArray()
|
||||
return this.copyOf().apply { sortDescending() }
|
||||
"""
|
||||
}
|
||||
}
|
||||
@@ -204,9 +214,7 @@ fun ordering(): List<GenericFunction> {
|
||||
}
|
||||
body {
|
||||
"""
|
||||
val sortedList = toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
return sortedList
|
||||
return toArrayList().apply { sortWith(comparator) }
|
||||
"""
|
||||
}
|
||||
|
||||
@@ -219,7 +227,7 @@ fun ordering(): List<GenericFunction> {
|
||||
return object : Sequence<T> {
|
||||
override fun iterator(): Iterator<T> {
|
||||
val sortedList = this@sortedWith.toArrayList()
|
||||
java.util.Collections.sort(sortedList, comparator)
|
||||
sortedList.sortWith(comparator)
|
||||
return sortedList.iterator()
|
||||
}
|
||||
}
|
||||
@@ -241,6 +249,17 @@ fun ordering(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("sortBy(crossinline selector: (T) -> R?)") {
|
||||
inline(true)
|
||||
only(Lists, ArraysOfObjects)
|
||||
doc { f -> """Sorts elements in the ${f.collection} in-place according to natural sort order of the value returned by specified [selector] function.""" }
|
||||
returns("Unit")
|
||||
typeParam("R : Comparable<R>")
|
||||
customReceiver(Lists) { "MutableList<T>" }
|
||||
|
||||
body { """if (size > 1) sortWith(compareBy(selector))""" }
|
||||
}
|
||||
|
||||
templates add f("sortedBy(crossinline selector: (T) -> R?)") {
|
||||
inline(true)
|
||||
returns("List<T>")
|
||||
@@ -262,6 +281,18 @@ fun ordering(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("sortByDescending(crossinline selector: (T) -> R?)") {
|
||||
inline(true)
|
||||
only(Lists, ArraysOfObjects)
|
||||
doc { f -> """Sorts elements in the ${f.collection} in-place descending according to natural sort order of the value returned by specified [selector] function.""" }
|
||||
returns("Unit")
|
||||
typeParam("R : Comparable<R>")
|
||||
customReceiver(Lists) { "MutableList<T>" }
|
||||
|
||||
body {
|
||||
"""if (size > 1) sortWith(compareByDescending(selector))""" }
|
||||
}
|
||||
|
||||
templates add f("sortedByDescending(crossinline selector: (T) -> R?)") {
|
||||
inline(true)
|
||||
returns("List<T>")
|
||||
@@ -283,128 +314,5 @@ fun ordering(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
// templates add f("sort()") {
|
||||
// doc {
|
||||
// """
|
||||
// Returns a sorted list of all elements.
|
||||
// """
|
||||
// }
|
||||
// returns("List<T>")
|
||||
// typeParam("T : Comparable<T>")
|
||||
// deprecate("This method may change its behavior soon. Use sorted() instead.")
|
||||
// deprecateReplacement("sorted()")
|
||||
// body {
|
||||
// """
|
||||
// val sortedList = toArrayList()
|
||||
// java.util.Collections.sort(sortedList)
|
||||
// return sortedList
|
||||
// """
|
||||
// }
|
||||
//
|
||||
// exclude(Sequences)
|
||||
// exclude(ArraysOfPrimitives)
|
||||
// exclude(ArraysOfObjects)
|
||||
// exclude(Strings)
|
||||
// }
|
||||
|
||||
// templates add f("sortDescending()") {
|
||||
// doc {
|
||||
// """
|
||||
// Returns a sorted list of all elements, in descending order.
|
||||
// """
|
||||
// }
|
||||
// deprecate("This method may change its behavior soon. Use sortedDescending() instead.")
|
||||
// deprecateReplacement("sortedDescending()")
|
||||
// returns("List<T>")
|
||||
// typeParam("T : Comparable<T>")
|
||||
// body {
|
||||
// """
|
||||
// val sortedList = toArrayList()
|
||||
// java.util.Collections.sort(sortedList, comparator { x, y -> y.compareTo(x) })
|
||||
// return sortedList
|
||||
// """
|
||||
// }
|
||||
//
|
||||
// exclude(Sequences)
|
||||
// exclude(ArraysOfPrimitives)
|
||||
// exclude(ArraysOfObjects)
|
||||
// exclude(Strings)
|
||||
// }
|
||||
|
||||
// templates add f("sortBy(crossinline order: (T) -> R)") {
|
||||
// inline(true)
|
||||
//
|
||||
// doc {
|
||||
// """
|
||||
// Returns a sorted list of all elements, ordered by results of specified [order] function.
|
||||
// """
|
||||
// }
|
||||
// deprecate("This method may change its behavior soon. Use sortedBy() instead.")
|
||||
// deprecateReplacement("sortedBy(order)")
|
||||
// returns("List<T>")
|
||||
// typeParam("R : Comparable<R>")
|
||||
// body {
|
||||
// """
|
||||
// val sortedList = toArrayList()
|
||||
// val sortBy: Comparator<T> = compareBy(order)
|
||||
// java.util.Collections.sort(sortedList, sortBy)
|
||||
// return sortedList
|
||||
// """
|
||||
// }
|
||||
//
|
||||
// exclude(Sequences)
|
||||
// exclude(ArraysOfPrimitives)
|
||||
// exclude(Strings)
|
||||
// }
|
||||
|
||||
|
||||
// templates add f("sortDescendingBy(crossinline order: (T) -> R)") {
|
||||
// inline(true)
|
||||
//
|
||||
// doc {
|
||||
// """
|
||||
// Returns a sorted list of all elements, in descending order by results of specified [order] function.
|
||||
// """
|
||||
// }
|
||||
// deprecate("This method may change its behavior soon. Use sortedByDescending() instead.")
|
||||
// deprecateReplacement("sortedByDescending(order)")
|
||||
// returns("List<T>")
|
||||
// typeParam("R : Comparable<R>")
|
||||
// body {
|
||||
// """
|
||||
// val sortedList = toArrayList()
|
||||
// val sortBy: Comparator<T> = compareByDescending(order)
|
||||
// java.util.Collections.sort(sortedList, sortBy)
|
||||
// return sortedList
|
||||
// """
|
||||
// }
|
||||
//
|
||||
// exclude(Sequences)
|
||||
// exclude(ArraysOfPrimitives)
|
||||
// exclude(Strings)
|
||||
// }
|
||||
|
||||
// templates add f("sortBy(comparator: Comparator<in T>)") {
|
||||
// doc {
|
||||
// """
|
||||
// Returns a list of all elements, sorted by the specified [comparator].
|
||||
// """
|
||||
// }
|
||||
// returns("List<T>")
|
||||
// deprecate("This method may change its behavior soon. Use sortedWith() instead.")
|
||||
// deprecateReplacement("sortedWith(comparator)")
|
||||
// body {
|
||||
// """
|
||||
// val sortedList = toArrayList()
|
||||
// java.util.Collections.sort(sortedList, comparator)
|
||||
// return sortedList
|
||||
// """
|
||||
// }
|
||||
//
|
||||
// exclude(Sequences)
|
||||
// exclude(ArraysOfPrimitives)
|
||||
// exclude(Strings)
|
||||
// }
|
||||
|
||||
return templates
|
||||
}
|
||||
Reference in New Issue
Block a user