Kotlin compareValuesBy Function

The compareValuesBy function in Kotlin is used to compare two values by multiple selectors and return an integer indicating their relative order. This function is part of the Kotlin standard library and belongs to the kotlin.comparisons package. It provides a convenient way to compare two values based on multiple properties.

Table of Contents

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

Introduction

The compareValuesBy function allows you to compare two values by one or more selectors and determine their order based on those properties. This is useful for scenarios where you need to compare two values based on multiple attributes.

compareValuesBy Function Syntax

The syntax for the compareValuesBy function is as follows:

fun <T> compareValuesBy(a: T?, b: T?, vararg selectors: (T) -> Comparable<*>?): Int 

Parameters:

  • a: The first value to be compared.
  • b: The second value to be compared.
  • selectors: One or more selector functions that extract the properties to be compared.

Returns:

  • Int: Returns a negative integer if a is less than b, zero if a is equal to b, and a positive integer if a is greater than b.

Understanding compareValuesBy

The compareValuesBy function compares two values based on the properties extracted by the selector functions. If the first property is equal, the next property is compared, and so on. If either of the values is null, null is considered to be less than any non-null value.

Examples

Basic Usage

To demonstrate the basic usage of compareValuesBy, we will compare two objects by a single property.

Example

import kotlin.comparisons.compareValuesBy data class Person(val name: String, val age: Int) fun main() { val person1 = Person("Amit", 30) val person2 = Person("Bhavna", 25) val result = compareValuesBy(person1, person2) { it.age } println("Comparing by age: $result") } 

Output:

Comparing by age: 1 

Comparing by Multiple Properties

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

Example

import kotlin.comparisons.compareValuesBy data class Person(val name: String, val age: Int, val city: String) fun main() { val person1 = Person("Amit", 30, "Mumbai") val person2 = Person("Amit", 30, "Delhi") val person3 = Person("Bhavna", 25, "Bangalore") val result1 = compareValuesBy(person1, person2, { it.age }, { it.city }) val result2 = compareValuesBy(person1, person3, { it.age }, { it.city }) println("Comparing person1 and person2 by age and city: $result1") println("Comparing person1 and person3 by age and city: $result2") } 

Output:

Comparing person1 and person2 by age and city: 1 Comparing person1 and person3 by age and city: 1 

Real-World Use Case

Sorting a List of Products by Price and Name

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

Example

import kotlin.comparisons.compareValuesBy 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 sortedProducts = products.sortedWith { a, b -> compareValuesBy(a, b, { it.price }, { it.name }) } println("Products sorted by price and name: $sortedProducts") } 

Output:

Products 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 compareValuesBy function in Kotlin, part of the kotlin.comparisons package, is used for comparing two values based on multiple properties. By understanding and using the compareValuesBy function, you can effectively compare and sort values in your Kotlin applications, ensuring that objects are ordered as desired based on their attributes.

Leave a Comment

Scroll to Top