Alex Row
Alex Row

Reputation: 65

Comparator for Pair in Kotlin

I can write comparator for specialized types in Kotlin
class Comparator() : kotlin.Comparator<Pair<Double, Int>>
But how can I write comparator using generics for all possible types that extend Comparable<...>?

Upvotes: 0

Views: 5547

Answers (2)

Ilya
Ilya

Reputation: 23115

You can use the following function to combine two comparators into a pair comparator which compares the first value of a pair with the first comparator and then the second value with the second comparator:

fun <T, U> pairComparator(
    firstComparator: Comparator<T>, 
    secondComparator: Comparator<U>
): Comparator<Pair<T, U>> = 
         compareBy(firstComparator) { p: Pair<T, U> -> p.first }
          .thenBy(secondComparator) { p: Pair<T, U> -> p.second }

Then you use it as following:

val source = listOf(1 to 2.3, 1 to 2.0, 0 to 3.0)
val result1 = source.sortedWith(pairComparator(naturalOrder(), naturalOrder()))
println(result1)  // [(0, 3.0), (1, 2.0), (1, 2.3)]

val result2 = source.sortedWith(pairComparator(naturalOrder(), reverseOrder()))
println(result2)  // [(0, 3.0), (1, 2.3), (1, 2.0)]

See the full code here: https://pl.kotl.in/BkMOzb8CX

Upvotes: 3

Sergio
Sergio

Reputation: 30645

You can create comparator, using helping functions - compareBy, compareByDescending, naturalOrder, reverseOrder https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.comparisons/index.html

For example:

val map = mapOf<Int, String>()
// ... add values to the map
val sortedMap: SortedMap<Int, String> = map.toSortedMap(compareByDescending { it })

And for your case:

val comparator = compareBy<Pair<Double, Int>> { it.first }

Custom comparator:

class CustomComparator<T: Comparable<T>> : Comparator<T> {
    override fun compare(o1: T, o2: T): Int {
        return o1.compareTo(o2)
    }
}

Upvotes: 3

Related Questions