Comparable interface in Kotlin

Last Updated : 12 Jul, 2025

In Kotlin, we can define new types using classes. Once we create these classes, we might want to compare the objects (instances) of these classes. To make this possible, Kotlin provides a useful tool called the Comparable interface, just like Java does. The Comparable interface allows us to define the way two objects should be compared. The main function that this interface provides is called compareTo(). But Kotlin also offers additional powerful features like extension functions, which make object comparison even easier and more flexible.

When we want to sort objects or check if one object is bigger or smaller than another, we use Comparable. For example, if we create a class called Rectangle, we may want to compare two rectangles by their area or any other property.

The compareTo() Function

This function compares the calling object with the passed object. It returns zero, if both are equal, a negative number if the passed object is greater, otherwise it returns a positive number.

abstract operator fun compareTo(other: T): Int

It compares the current object (this) with another object (other) and returns 0 if both objects are equal, a negative number if the current object is less than the other object, and a positive number if the current object is greater than the other object. In Kotlin, because of operator overloading, we can also use normal comparison operators like

>, <, >=, <=

This means when a class implements Comparable, its objects can be compared just like numbers or strings.

Extension Functions for Comparable

1. coerceAtLeast()

This function checks whether the calling object is greater than a certain minimum object. It returns current object if it is greater otherwise returns the minimum object

fun <T : Comparable<T>> T.coerceAtLeast(minimumValue: T): T

Example:

val number = 5
println(number.coerceAtLeast(10)) // Output: 10


2. coerceAtMost()

This function checks whether the calling object is smaller than the given maximum object. It returns the current object if its smaller, otherwise returns the maximum object.

fun <T : Comparable> T.coerceAtMost(maximumValue: T): T

Example:

val number = 15
println(number.coerceAtMost(10)) // Output: 10


3. coerceIn()

This function checks whether the calling object is within a certain minimum and maximum value. It returns the object if it is in the range, otherwise returns minimum if object is less than the minimum, else returns the maximum.

fun <T : Comparable<T>> T.coerceIn(
minimumValue: T?,
maximumValue: T?
): T

Example:

val number = 15
println(number.coerceIn(10, 20)) // Output: 15
println(number.coerceIn(16, 20)) // Output: 16

Example: Using Comparable with a Class:

Kotlin
data class Rectangle(val length: Int, val breadth: Int): Comparable<Rectangle> {
    override fun compareTo(other: Rectangle): Int {
        val thisArea = this.length * this.breadth
        val otherArea = other.length * other.breadth
        return thisArea.compareTo(otherArea)
    }
}

fun main() {
    val rectangleOne = Rectangle(10, 20)
    val rectangleTwo = Rectangle(5, 15)
    val minRectangle = Rectangle(2, 2)
    val maxRectangle = Rectangle(15, 30)

    println("Is rectangle one greater than or equal to rectangle two? ${rectangleOne >= rectangleTwo}")
    println("Is rectangle one greater than the minimum sized rectangle? ${rectangleOne >= minRectangle}")
    println("Is rectangle two smaller than the maximum sized rectangle? ${rectangleTwo <= maxRectangle}")
    println("Is rectangle one within the bounds? ${rectangleOne in minRectangle..maxRectangle}")
}

Output:

Is rectangle one greater than or equal to rectangle two? true
Is rectangle one greater than the minimum sized rectangle? true
Is rectangle two smaller than the maximum sized rectangle? false
Is rectangle one within the bounds? true


4. rangeTo()

In Kotlin, rangeTo() lets us check whether a value falls inside a range or not. It returns a ClosedRange, which includes both the starting and ending values.

operator fun <T : Comparable<T>> T.rangeTo(that: T): ClosedRange<T>

Example:

Kotlin
fun main() {
    val range = 1..1000
    println("Range: $range")
    println("Is 55 within the range? ${55 in range}")
    println("Is 100000 within the range? ${100000 in range}")
}

Output:

Range: 1..1000
Is 55 within the range? true
Is 100000 within the range? false


Comment
Article Tags:

Explore