The sortedByDescending function in Kotlin is used to sort the elements in a sequence based on a specified selector function in descending order. It is part of the Kotlin standard library and allows you to sort elements according to custom criteria in reverse order.
Table of Contents
- Introduction
sortedByDescendingFunction Syntax- Understanding
sortedByDescending - Examples
- Basic Usage
- Sorting Strings by Length in Descending Order
- Using
sortedByDescendingwith Custom Objects - Chaining
sortedByDescendingwith Other Functions
- Real-World Use Case
- Conclusion
Introduction
The sortedByDescending function allows you to sort the elements of a sequence based on a custom selector function in descending order. This is useful for scenarios where you need to arrange elements in reverse order according to specific criteria, enabling more precise data manipulation and presentation.
sortedByDescending Function Syntax
The syntax for the sortedByDescending function is as follows:
fun <T, R : Comparable<R>> Sequence<T>.sortedByDescending(selector: (T) -> R?): Sequence<T> Parameters:
selector: A lambda function that defines the criteria for sorting the elements.
Returns:
- A sequence with the elements sorted in descending order based on the specified criteria.
Understanding sortedByDescending
The sortedByDescending function takes a sequence and sorts its elements according to the value returned by the selector function for each element, in descending order. This operation is terminal, meaning it triggers the evaluation of the sequence and produces a sorted sequence.
Examples
Basic Usage
To demonstrate the basic usage of sortedByDescending, we will create a sequence of integers and sort them in descending order by their absolute values.
Example
fun main() { val numbers = sequenceOf(-3, 2, -5, 4, -1) val sortedNumbers = numbers.sortedByDescending { it.absoluteValue } println(sortedNumbers.toList()) // Output: [-5, 4, -3, 2, -1] } Output:
[-5, 4, -3, 2, -1] Sorting Strings by Length in Descending Order
This example shows how to sort a sequence of strings by their length in descending order.
Example
fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha") val sortedNames = names.sortedByDescending { it.length } println(sortedNames.toList()) // Output: [Bhaskar, Deepak, Chitra, Arjun, Esha] } Output:
[Bhaskar, Deepak, Chitra, Arjun, Esha] Using sortedByDescending with Custom Objects
You can use the sortedByDescending function to sort custom objects based on specific properties in descending order.
Example
data class Person(val name: String, val age: Int) fun main() { val people = sequenceOf( Person("Arjun", 25), Person("Bhaskar", 30), Person("Chitra", 22), Person("Deepak", 28), Person("Esha", 26) ) val sortedByAgeDescending = people.sortedByDescending { it.age } println(sortedByAgeDescending.toList()) // Output: [Person(name=Bhaskar, age=30), Person(name=Deepak, age=28), Person(name=Esha, age=26), Person(name=Arjun, age=25), Person(name=Chitra, age=22)] } Output:
[Person(name=Bhaskar, age=30), Person(name=Deepak, age=28), Person(name=Esha, age=26), Person(name=Arjun, age=25), Person(name=Chitra, age=22)] Chaining sortedByDescending with Other Functions
The sortedByDescending function can be chained with other sequence functions to perform complex operations and sorting.
Example
fun main() { val numbers = sequenceOf(5, 3, 1, 4, 2) val result = numbers.filter { it % 2 == 0 } .sortedByDescending { it } .map { it * 2 } println(result.toList()) // Output: [8, 4] } Output:
[8, 4] Real-World Use Case
Sorting Products by Price in Descending Order
In real-world applications, the sortedByDescending function can be used to sort products by price, name, or other attributes in descending order.
Example
data class Product(val name: String, val price: Double) fun main() { val products = sequenceOf( Product("Laptop", 999.99), Product("Smartphone", 499.99), Product("Tablet", 299.99), Product("Smartwatch", 199.99) ) val sortedByPriceDescending = products.sortedByDescending { it.price } println(sortedByPriceDescending.toList()) // Output: [Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99), Product(name=Tablet, price=299.99), Product(name=Smartwatch, price=199.99)] } Output:
[Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99), Product(name=Tablet, price=299.99), Product(name=Smartwatch, price=199.99)] Conclusion
The sortedByDescending function in Kotlin provides used for sorting elements in a sequence based on custom criteria in descending order. By understanding and using the sortedByDescending function, you can efficiently organize data in your Kotlin applications, making them more flexible and responsive to user needs.