Change null handling in compareValuesBy(a, b, functions). Provide nullsFirst() and nullsLast() to extend Comparator<T> to Comparator<T?>.

This commit is contained in:
Ilya Gorbunov
2015-07-28 18:36:38 +03:00
parent 63110dcdf8
commit d49a1973e5
4 changed files with 76 additions and 15 deletions
@@ -827,9 +827,9 @@ class ArraysTest {
test fun sortedNullableBy() {
fun String.nullIfEmpty() = if (isEmpty()) null else this
arrayOf(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() ) }
}
}