The reversed function in Kotlin is used to obtain a comparator that reverses the order of an existing comparator. This function is part of the Kotlin standard library and belongs to the kotlin.comparisons package. It provides a convenient way to reverse the natural order or any custom order defined by a comparator.
Table of Contents
- Introduction
reversedFunction Syntax- Understanding
reversed - Examples
- Basic Usage
- Reversing a Custom Comparator
- Real-World Use Case
- Conclusion
Introduction
The reversed function allows you to reverse the order of an existing comparator. This is useful for scenarios where you need to sort or compare objects in descending order.
reversed Function Syntax
The syntax for the reversed function is as follows:
fun <T> Comparator<T>.reversed(): Comparator<T>
Parameters:
- The
reversedfunction does not take any parameters.
Returns:
Comparator<T>: A comparator that reverses the order of the original comparator.
Understanding reversed
The reversed function creates a new comparator that reverses the order of the original comparator. If the original comparator sorts objects in ascending order, the new comparator will sort them in descending order.
Examples
Basic Usage
To demonstrate the basic usage of reversed, we will create a list of integers and sort it in descending order using the reversed function.
Example
fun main() {
val numbers = listOf(5, 3, 8, 1, 2)
val sortedNumbersDescending = numbers.sortedWith(compareBy { it }.reversed())
println("Sorted numbers in descending order: $sortedNumbersDescending")
}
Output:
Sorted numbers in descending order: [8, 5, 3, 2, 1]
Reversing a Custom Comparator
This example shows how to use reversed to reverse a custom comparator for a list of custom objects.
Example
data class Person(val name: String, val age: Int)
fun main() {
val people = listOf(
Person("Amit", 30),
Person("Bhavna", 25),
Person("Chirag", 35)
)
val sortedPeopleDescending = people.sortedWith(compareBy { it.age }.reversed())
println("Sorted people by age in descending order: $sortedPeopleDescending")
}
Output:
Sorted people by age in descending order: [Person(name=Chirag, age=35), Person(name=Amit, age=30), Person(name=Bhavna, age=25)]
Real-World Use Case
Sorting Products by Price in Descending Order
In real-world applications, the reversed function can be used to sort a list of products by price in descending order.
Example
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)
)
val sortedProductsDescending = products.sortedWith(compareBy { it.price }.reversed())
println("Products sorted by price in descending order: $sortedProductsDescending")
}
Output:
Products sorted by price in descending order: [Product(name=Laptop, price=75000.0), Product(name=Tablet, price=30000.0), Product(name=Smartphone, price=25000.0)]
Conclusion
The reversed function in Kotlin is used for reversing the order of an existing comparator. By understanding and using the reversed function, you can effectively sort and compare collections in descending order in your Kotlin applications, ensuring that objects are ordered as desired based on their attributes.