JS: drop Collections.sort

This commit is contained in:
Alexander Udalov
2016-11-02 12:12:13 +03:00
parent 30a7790dca
commit 316fbd820b
4 changed files with 25 additions and 27 deletions
@@ -260,14 +260,16 @@ private fun MutableCollection<*>.retainNothing(): Boolean {
/**
* Sorts elements in the list in-place according to their natural sort order.
* */
public fun <T: Comparable<T>> MutableList<T>.sort(): Unit {
*/
@kotlin.jvm.JvmVersion
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].
* Sorts elements in the list in-place according to the order specified with [comparator].
*/
@kotlin.jvm.JvmVersion
public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
if (size > 1) java.util.Collections.sort(this, comparator)
}
@@ -6,11 +6,8 @@ import kotlin.test.*
import org.junit.Test as test
import kotlin.comparisons.*
fun <T> List<T>.toArrayList() = this.toCollection(ArrayList<T>())
class JsCollectionsTest {
val TEST_LIST = arrayOf(2, 0, 9, 7, 1).toList()
val SORTED_TEST_LIST = arrayOf(0, 1, 2, 7, 9).toList()
val MAX_ELEMENT = 9
val COMPARATOR = Comparator { x: Int, y: Int -> if (x > y) 1 else if (x < y) -1 else 0 }
@@ -18,18 +15,6 @@ class JsCollectionsTest {
assertEquals(MAX_ELEMENT, Collections.max(TEST_LIST, COMPARATOR))
}
@test fun sort() {
val list = TEST_LIST.toArrayList()
Collections.sort(list)
assertEquals(SORTED_TEST_LIST, list)
}
@test fun sortWithComparator() {
val list = TEST_LIST.toArrayList()
Collections.sort(list, COMPARATOR)
assertEquals(SORTED_TEST_LIST, list)
}
@test fun collectionToArray() {
val array = TEST_LIST.toTypedArray()
assertEquals(array.toList(), TEST_LIST)