Another approach for sorting nulls.
This commit is contained in:
@@ -39,7 +39,7 @@ class OrderingTest {
|
||||
val v2: Item? = null
|
||||
val diff = compareValuesBy(v1, v2) { it?.rating }
|
||||
assertTrue(diff > 0)
|
||||
val diff2 = compareBy<Item> { it.rating }.thenBy { it.name }.nullsLast().compare(v1, v2)
|
||||
val diff2 = nullsLast(compareBy<Item> { it.rating }.thenBy { it.name }).compare(v1, v2)
|
||||
assertTrue(diff2 < 0)
|
||||
}
|
||||
|
||||
|
||||
@@ -827,9 +827,9 @@ class ArraysTest {
|
||||
test fun sortedNullableBy() {
|
||||
fun String.nullIfEmpty() = if (isEmpty()) null else this
|
||||
arrayOf(null, "").let {
|
||||
expect(listOf(null, "")) { it.sortedWith(compareBy<String> { it }.nullsFirst()) }
|
||||
expect(listOf("", null)) { it.sortedWith(compareByDescending<String> { it }.nullsLast()) }
|
||||
expect(listOf("", null)) { it.sortedWith(compareByDescending<String> { it.nullIfEmpty() }.nullsLast() ) }
|
||||
expect(listOf(null, "")) { it.sortedWith(nullsFirst(compareBy { it })) }
|
||||
expect(listOf("", null)) { it.sortedWith(nullsLast(compareByDescending { it })) }
|
||||
expect(listOf("", null)) { it.sortedWith(nullsLast(compareByDescending { it.nullIfEmpty() })) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -572,13 +572,19 @@ class CollectionTest {
|
||||
|
||||
test fun sortedNullableBy() {
|
||||
fun String.nullIfEmpty() = if (isEmpty()) null else this
|
||||
listOf(null, "").let {
|
||||
// expect(listOf(null, "")) { it.sortedBy { it.orEmpty()}}
|
||||
// expect(listOf("", null)) { it.sortedByDescending { it.orEmpty() }}
|
||||
// expect(listOf("", null)) { it.sortedByDescending { it?.nullIfEmpty() }}
|
||||
expect(listOf(null, "")) { it.sortedWith(compareBy<String> { it }.nullsFirst()) }
|
||||
expect(listOf("", null)) { it.sortedWith(compareByDescending<String> { it }.nullsLast()) }
|
||||
expect(listOf("", null)) { it.sortedWith(compareByDescending<String> { it.nullIfEmpty() }.nullsLast() ) }
|
||||
listOf(null, "", "a").let {
|
||||
expect(listOf(null, "", "a")) { it.sortedWith(nullsFirst(compareBy { it })) }
|
||||
expect(listOf("a", "", null)) { it.sortedWith(nullsLast(compareByDescending { it })) }
|
||||
expect(listOf(null, "a", "")) { it.sortedWith(nullsFirst(compareByDescending { it.nullIfEmpty() })) }
|
||||
}
|
||||
}
|
||||
|
||||
test fun sortedByNullable() {
|
||||
fun String.nonEmptyLength() = if (isEmpty()) null else length()
|
||||
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()}))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user