Parameterless nullsFirst and nullsLast and misc comparator combining functions.

This commit is contained in:
Ilya Gorbunov
2015-08-06 17:10:54 +03:00
parent 5a4e598ba7
commit 7d33599fc2
3 changed files with 131 additions and 13 deletions
+14
View File
@@ -53,6 +53,20 @@ class OrderingTest {
assertEquals(v1, items[1])
}
Test fun combineComparators() {
val byName = compareBy<Item> { it.name }
val byRating = compareBy<Item> { it.rating }
val v3 = Item(v1.name, v1.rating + 1)
val v4 = Item(v2.name + "_", v2.rating)
assertTrue( (byName then byRating).compare(v1, v2) > 0 )
assertTrue( (byName then byRating).compare(v1, v3) < 0 )
assertTrue( (byName thenDescending byRating).compare(v1, v3) > 0 )
assertTrue( (byRating then byName).compare(v1, v2) < 0 )
assertTrue( (byRating then byName).compare(v4, v2) > 0 )
assertTrue( (byRating thenDescending byName).compare(v4, v2) < 0 )
}
Test fun sortByThenBy() {
val comparator = compareBy<Item> { it.rating } thenBy { it.name }
@@ -584,7 +584,7 @@ class CollectionTest {
listOf("", "sort", "abc").let {
assertEquals(listOf("", "abc", "sort"), it.sortedBy { it.nonEmptyLength() })
assertEquals(listOf("sort", "abc", ""), it.sortedByDescending { it.nonEmptyLength() })
assertEquals(listOf("abc", "sort", ""), it.sortedWith(compareBy(nullsLast(compareBy<Int> {it})) { it.nonEmptyLength()}))
assertEquals(listOf("abc", "sort", ""), it.sortedWith(compareBy(nullsLast<Int>()) { it.nonEmptyLength()}))
}
}