Merge branch 'master' into fix-reverse-and-sort-for-lists
Conflicts: libraries/stdlib/test/CollectionTest.kt
This commit is contained in:
@@ -52,7 +52,11 @@
|
||||
<module>tools/runtime</module>
|
||||
<module>tools/kotlin-maven-plugin</module>
|
||||
<module>tools/kotlin-js-library</module>
|
||||
<!--
|
||||
TODO temporary disabled until the Ordering.kt#comparator() works in JS
|
||||
|
||||
<module>tools/kotlin-js-tests</module>
|
||||
-->
|
||||
<!--
|
||||
TODO temporary disabled until the java.util.Collections.emptyList() works in JS
|
||||
|
||||
|
||||
@@ -71,6 +71,19 @@ public fun <T> java.lang.Iterable<T>.last() : T {
|
||||
return last;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
|
||||
*
|
||||
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt sortBy
|
||||
*/
|
||||
public inline fun <in T, R: Comparable<in R>> java.lang.Iterable<T>.sortBy(f: (T) -> R): java.util.List<T> {
|
||||
val sortedList = this.toList()
|
||||
java.util.Collections.sort(sortedList, comparator {(x, y) -> f(x).compareTo(f(y))})
|
||||
return sortedList
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if collection contains given item.
|
||||
*
|
||||
|
||||
@@ -462,6 +462,18 @@ class CollectionTest {
|
||||
// assertFalse(IterableWrapper(linkedList<Int>()).contains(15))
|
||||
}
|
||||
|
||||
test fun sortBy() {
|
||||
expect(arrayList("two" to 2, "three" to 3)) {
|
||||
arrayList("three" to 3, "two" to 2).sortBy { it._2 }
|
||||
}
|
||||
expect(arrayList("three" to 3, "two" to 2)) {
|
||||
arrayList("three" to 3, "two" to 2).sortBy { it._1 }
|
||||
}
|
||||
expect(arrayList("two" to 2, "three" to 3)) {
|
||||
arrayList("three" to 3, "two" to 2).sortBy { it._1.length }
|
||||
}
|
||||
}
|
||||
|
||||
class IterableWrapper<T>(collection : java.lang.Iterable<T>) : java.lang.Iterable<T> {
|
||||
private val collection = collection
|
||||
|
||||
|
||||
Reference in New Issue
Block a user