0Pricing
Kotlin Academy · Lesson

Comparison Operators

compareTo and equals.

Comparison Operators is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Comparing Custom Types

To use <, >, <=, >= on your type, define an operator fun compareTo that returns an Int.

data class Money(val cents: Int) {
    operator fun compareTo(o: Money) = cents - o.cents
}

fun main() {
    println(Money(500) > Money(200))
}

The compareTo Contract

compareTo returns a negative number if the receiver is smaller, zero if equal, and positive if larger.

data class Version(val n: Int) {
    operator fun compareTo(o: Version) = n - o.n
}

fun main() {
    println(Version(2).compareTo(Version(5)))
}

Implementing Comparable

Implementing the Comparable<T> interface is the idiomatic approach; its compareTo is already an operator.

data class Score(val value: Int) : Comparable<Score> {
    override fun compareTo(other: Score) = value - other.value
}

fun main() {
    println(Score(10) < Score(20))
}

Sorting with Comparable

Once a type is Comparable, collection functions like sorted() work automatically.

data class Player(val rank: Int) : Comparable<Player> {
    override fun compareTo(other: Player) = rank - other.rank
}

fun main() {
    val sorted = listOf(Player(3), Player(1), Player(2)).sorted()
    println(sorted.map { it.rank })
}

Using compareValuesBy

For multi-field comparisons, compareValuesBy compares by each selector in order.

data class Person(val age: Int, val name: String) : Comparable<Person> {
    override fun compareTo(other: Person) =
        compareValuesBy(this, other, { it.age }, { it.name })
}

fun main() {
    println(Person(30, "Al") < Person(30, "Bo"))
}

Equality with equals

The == operator calls equals, not compareTo. Override equals to control value equality.

class Id(val value: Int) {
    override fun equals(other: Any?) =
        other is Id && other.value == value
    override fun hashCode() = value
}

fun main() {
    println(Id(7) == Id(7))
}

equals and hashCode Together

If you override equals, you must also override hashCode so equal objects share a hash, keeping hash-based collections correct.

class Tag(val name: String) {
    override fun equals(other: Any?) = other is Tag && other.name == name
    override fun hashCode() = name.hashCode()
}

fun main() {
    val set = hashSetOf(Tag("a"), Tag("a"))
    println(set.size)
}

Data Classes Do It for You

A data class auto-generates equals and hashCode from its properties, so == compares by value.

data class Coord(val x: Int, val y: Int)

fun main() {
    println(Coord(1, 2) == Coord(1, 2))
}

Equality vs Identity

== calls equals (value equality); === checks reference identity. They can differ.

data class Box(val n: Int)

fun main() {
    val a = Box(1)
    val b = Box(1)
    println(a == b)
    println(a === b)
}

Comparing in Conditions

With compareTo defined, your type works in if and when like numbers.

data class Weight(val g: Int) : Comparable<Weight> {
    override fun compareTo(other: Weight) = g - other.g
}

fun main() {
    val w = Weight(50)
    println(if (w > Weight(40)) "heavy" else "light")
}

maxOf and minOf

Comparable types work with maxOf and minOf.

data class T(val v: Int) : Comparable<T> {
    override fun compareTo(other: T) = v - other.v
}

fun main() {
    println(maxOf(T(3), T(8)).v)
}

Quick Check

Test your understanding of comparison operators.

Recap

You learned comparison operators:

  • compareTo backs <, >, <=, >= and returns negative/zero/positive.
  • Implement Comparable<T> for idiomatic ordering and sorting.
  • == uses equals; always pair it with hashCode.
  • Data classes generate equals/hashCode automatically.
data class S(val v: Int) : Comparable<S> {
    override fun compareTo(other: S) = v - other.v
}

fun main() {
    println(S(2) < S(5))
}

Frequently asked questions

Is the “Comparison Operators” lesson free?

Yes — the full text of “Comparison Operators” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Comparison Operators”?

compareTo and equals. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Comparison Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Operator Functions
  2. Comparison Operators
  3. Index and Invoke
  4. Best Practices
← Back to Kotlin Academy