Open In App

Sort a List by Specified Comparator in Kotlin

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Sorting a list is one of the most common operations we perform when working with collections. In this article, we’ll learn how to sort a list by a specified comparator in Kotlin especially when the list contains objects of a custom class.

Kotlin Comparator

In Kotlin, a Comparator is an interface that helps us define how objects should be ordered. It tells Kotlin which property or rule to follow when sorting objects. For example, if we have a list of Person objects, and we want to sort them by their age, the comparator will help us do that. There are two ways to create a Comparator object:

  • Using compareBy() and thenBy() functions
  • Creating a custom Comparator manually

If we want to sort in the opposite (descending) order, we can also use the reversed() function on the comparator.

Read more here: Comparator in Kotlin

Sorting a List of Custom Objects

Let’s understand this better with an example. Suppose we have a simple Person class with two properties: name and age.

data class Person(val name: String, val age: Int)

Now we will sort a list of Person objects using different ways.

1. Sort Using sortedBy()

We can use the sortedBy() function to sort the list based on a specific property. For example, age.

Example:

Kotlin
fun main() {
    val people = listOf(
        Person("Alice", 25),
        Person("Bob", 20),
        Person("Charlie", 30)
    )
    
    val sortedByAge = people.sortedBy { it.age }
    println(sortedByAge)
}

Output:

[Person(name=Bob, age=20), Person(name=Alice, age=25), Person(name=Charlie, age=30)]

In this example, the list is sorted in ascending order by age.


2. Sort Using sortedWith() and a Custom Comparator

We can also create our own custom comparator using Comparator.

Example:

Kotlin
fun main() {
    val people = listOf(
        Person("Alice", 25),
        Person("Bob", 20),
        Person("Charlie", 30)
    )
    
    val customComparator = Comparator<Person> { p1, p2 ->
        p1.age - p2.age
    }
    
    val sortedList = people.sortedWith(customComparator)
    println(sortedList)
}

Output:

[Person(name=Bob, age=20), Person(name=Alice, age=25), Person(name=Charlie, age=30)]

This way, we define exactly how two Person objects should be compared.


3. Sort in Descending Order Using reversed()

To sort in descending order, we can simply use reversed() on the comparator.

Example:

Kotlin
fun main(){
    val people = listOf(
        Person("Alice", 25),
        Person("Bob", 20),
        Person("Charlie", 30)
    )
    
    val descending = people.sortedBy { it.age }.reversed()
    println(descending)
}

Output:

[Person(name=Charlie, age=30), Person(name=Alice, age=25), Person(name=Bob, age=20)]


4. Sort by Multiple Fields Using compareBy() and thenBy()

What if two people have the same age, and we want to sort them by name as a tie-breaker. We can use compareBy() with thenBy().

Example:

Kotlin
fun main() {
    val people = listOf(
        Person("Alice", 25),
        Person("Bob", 20),
        Person("Charlie", 30)
    )

    val multiSort = people.sortedWith(
        compareBy<Person> { it.age }.thenBy { it.name }
    )
    println(multiSort)
}

Output:

[Person(name=Bob, age=20), Person(name=Alice, age=25), Person(name=Charlie, age=30)]

This first sorts by age, and then by name if the ages are the same.


sortedBy() Internal Workings

Under the hood, sortedBy() uses the sortedWith() method along with a comparator.

When we write:

people.sortedBy { it.age }

It actually does this internally:

people.sortedWith(compareBy { it.age })

This is why sortedBy() is often called syntactic sugar, because it makes the code cleaner and easier to read, but does the same thing.


Similar Reads