The then function in Kotlin is used to chain multiple comparators together to create a composite comparator. This function is part of the Kotlin standard library and belongs to the kotlin.comparisons package. It allows you to perform secondary comparisons when the primary comparison results in equality.
Table of Contents
- Introduction
thenFunction Syntax- Understanding
then - Examples
- Basic Usage
- Chaining Multiple Comparators
- Real-World Use Case
- Conclusion
Introduction
The then function allows you to combine multiple comparators, so that if the primary comparator considers two objects equal, the secondary comparator is used to determine the order. This is useful for scenarios where you need to sort or compare objects based on multiple criteria.
then Function Syntax
The syntax for the then function is as follows:
infix fun <T> Comparator<T>.then(comparator: Comparator<in T>): Comparator<T>
Parameters:
comparator: The secondary comparator to be used if the primary comparator considers two objects equal.
Returns:
Comparator<T>: A composite comparator that performs the primary comparison first, followed by the secondary comparison if necessary.
Understanding then
The then function creates a composite comparator that first uses the primary comparator for comparison. If the primary comparator considers two objects equal, the secondary comparator provided to the then function is used to determine the order.
Examples
Basic Usage
To demonstrate the basic usage of then, we will create a list of custom objects and sort them using a composite comparator.
Example
import kotlin.comparisons.compareBy
import kotlin.comparisons.then
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 comparator = compareBy<Person> { it.age } then compareBy { it.city }
val sortedPeople = people.sortedWith(comparator)
println("Sorted people by age, then by city: $sortedPeople")
}
Output:
Sorted people by age, then by city: [Person(name=Bhavna, age=25, city=Delhi), Person(name=Divya, age=25, city=Bangalore), Person(name=Chirag, age=30, city=Ahmedabad), Person(name=Amit, age=30, city=Mumbai)]
Chaining Multiple Comparators
This example shows how to chain multiple comparators to sort a list of custom objects by three criteria.
Example
import kotlin.comparisons.compareBy
import kotlin.comparisons.then
data class Employee(val name: String, val department: String, val age: Int, val salary: Double)
fun main() {
val employees = listOf(
Employee("Amit", "HR", 30, 50000.0),
Employee("Bhavna", "Finance", 25, 60000.0),
Employee("Chirag", "HR", 30, 55000.0),
Employee("Divya", "Finance", 25, 50000.0)
)
val comparator = compareBy<Employee> { it.department }
.then(compareBy { it.age })
.then(compareBy { it.salary })
val sortedEmployees = employees.sortedWith(comparator)
println("Sorted employees by department, then by age, then by salary: $sortedEmployees")
}
Output:
Sorted employees by department, then by age, then by salary: [Employee(name=Divya, department=Finance, age=25, salary=50000.0), Employee(name=Bhavna, department=Finance, age=25, salary=60000.0), Employee(name=Amit, department=HR, age=30, salary=50000.0), Employee(name=Chirag, department=HR, age=30, salary=55000.0)]
Real-World Use Case
Sorting Students by Grade, then by Name
In real-world applications, the then function can be used to sort a list of students by grade and then by name.
Example
import kotlin.comparisons.compareBy
import kotlin.comparisons.then
data class Student(val name: String, val grade: String, val age: Int)
fun main() {
val students = listOf(
Student("Amit", "A", 15),
Student("Bhavna", "B", 14),
Student("Chirag", "A", 16),
Student("Divya", "B", 15)
)
val comparator = compareBy<Student> { it.grade } then compareBy { it.name }
val sortedStudents = students.sortedWith(comparator)
println("Sorted students by grade, then by name: $sortedStudents")
}
Output:
Sorted students by grade, then by name: [Student(name=Amit, grade=A, age=15), Student(name=Chirag, grade=A, age=16), Student(name=Bhavna, grade=B, age=14), Student(name=Divya, grade=B, age=15)]
Conclusion
The then function in Kotlin is used for creating composite comparators that allow for secondary comparisons when the primary comparison results in equality. By understanding and using the then function, you can effectively sort and compare collections based on multiple criteria in your Kotlin applications, ensuring that objects are ordered as desired based on their attributes.