Some sort of no-op comparator would be nice for st...
# stdlib
d
Some sort of no-op comparator would be nice for starting a comparator chain.
compareBy
requires to much type info up front, at least the overload that takes a selector.
i
Interesting. Could you show how you'd stack comparators on top of the proposed comparator vs. how you do it now with
compareBy
?
d
Given I have a
List<Pair<String, String?>>
Right now I have to do this.
Copy code
val currentComparator = compareBy<Pair<String, String?>, String?>(nullsLast()) { it.second }
    .thenBy { it.first }
If stdlib had something like this.
Copy code
object NoopComparator : Comparator<Nothing> {
    override fun compare(p0: Nothing?, p1: Nothing?): Int {
        return 0
    }
}

fun <T> noop(): Comparator<T> {
    return NoopComparator as Comparator<T>
}
Then I could do this.
Copy code
val idealComparator = noop<Pair<String, String?>>()
    .thenBy(nullsLast()) { it.second }
    .thenBy { it.first }
A workaround I guess is this but it's distracting in code reviews.
Copy code
val alternativeComparator = compareBy<Pair<String, String?>> {""}
    .thenBy(nullsLast()) { it.second }
    .thenBy { it.first }
I can't quite justify adding
NoopComparator
to my codebase when I could just add another type param. But I guess that's besides the point now.
(I'm not attached to the
noop
name)
e
Copy code
compareBy(nullsLast()) { it: Pair<String, String?> -> it.second }
compareBy(nullsLast(), Pair<String, String?>::second)
still a bit wordy but less than your current?
d
Hmm, the first one is seems like what I'm looking for.