Kotlin compareBy Function

The compareBy function in Kotlin is used to create a comparator that compares objects by multiple properties. This function is part of the Kotlin standard library and belongs to the kotlin.comparisons package. It provides a convenient way to define custom ordering for objects based on their properties.

Table of Contents

  1. Introduction
  2. compareBy Function Syntax
  3. Understanding compareBy
  4. Examples
    • Basic Usage
    • Comparing by Multiple Properties
  5. Real-World Use Case
  6. Conclusion

Introduction

The compareBy function allows you to create a comparator that can be used to sort collections of objects by one or more properties. This is useful for scenarios where you need to sort objects in a specific order based on their attributes.

compareBy Function Syntax

The syntax for the compareBy function is as follows:

fun <T> compareBy(vararg selectors: (T) -> Comparable<*>?): Comparator<T> 

Parameters:

  • selectors: One or more selector functions that extract the properties to be compared.

Returns:

  • Comparator<T>: A comparator that compares objects by the specified properties.

Understanding compareBy

The compareBy function creates a comparator that compares objects by the properties extracted by the selector functions. The comparator compares the objects in the order of the selector functions. If the first property is equal, the next property is compared, and so on.

Examples

Basic Usage

To demonstrate the basic usage of compareBy, we will create a list of objects and sort them by a single property.

Example

import kotlin.comparisons.compareBy data class Person(val name: String, val age: Int) fun main() { val people = listOf( Person("Amit", 30), Person("Bhavna", 25), Person("Chirag", 35) ) val sortedByAge = people.sortedWith(compareBy { it.age }) println("Sorted by age: $sortedByAge") } 

Output:

Sorted by age: [Person(name=Bhavna, age=25), Person(name=Amit, age=30), Person(name=Chirag, age=35)] 

Comparing by Multiple Properties

This example shows how to use compareBy to compare objects by multiple properties.

Example

import kotlin.comparisons.compareBy data class Person(val name: String, val age: Int, val city: String) fun main() { val people = listOf( Person("Amit", 30, "Mumbai"), Person("Bhavna", 25, "Delhi"), Person("Chirag", 30, "Ahmedabad"), Person("Divya", 25, "Bangalore") ) val sortedByAgeAndCity = people.sortedWith(compareBy({ it.age }, { it.city })) println("Sorted by age and city: $sortedByAgeAndCity") } 

Output:

Sorted by age and city: [Person(name=Divya, age=25, city=Bangalore), Person(name=Bhavna, age=25, city=Delhi), Person(name=Chirag, age=30, city=Ahmedabad), Person(name=Amit, age=30, city=Mumbai)] 

Real-World Use Case

Sorting Products by Price and Name

In real-world applications, the compareBy function can be used to sort a list of products by price and then by name.

Example

import kotlin.comparisons.compareBy data class Product(val name: String, val price: Double) fun main() { val products = listOf( Product("Laptop", 75000.0), Product("Smartphone", 25000.0), Product("Tablet", 30000.0), Product("Smartwatch", 25000.0) ) val sortedByPriceAndName = products.sortedWith(compareBy({ it.price }, { it.name })) println("Sorted by price and name: $sortedByPriceAndName") } 

Output:

Sorted by price and name: [Product(name=Smartphone, price=25000.0), Product(name=Smartwatch, price=25000.0), Product(name=Tablet, price=30000.0), Product(name=Laptop, price=75000.0)] 

Conclusion

The compareBy function in Kotlin, part of the kotlin.comparisons package, is used for creating comparators that sort objects by multiple properties. By understanding and using the compareBy function, you can effectively manage and manipulate collections in your Kotlin applications, ensuring that objects are sorted in the desired order based on their attributes.

Leave a Comment

Scroll to Top