The sorted function in Kotlin is used to sort the elements in a sequence. It is part of the Kotlin standard library and allows you to arrange the elements of a sequence in natural order or according to a specified comparator.
Table of Contents
- Introduction
sortedFunction Syntax- Understanding
sorted - Examples
- Basic Usage
- Sorting Strings
- Using
sortedwith Custom Comparators - Chaining
sortedwith Other Functions
- Real-World Use Case
- Conclusion
Introduction
The sorted function allows you to sort the elements of a sequence. This is useful for scenarios where you need to arrange elements in a specific order, enabling easier data manipulation and presentation.
sorted Function Syntax
The syntax for the sorted function is as follows:
fun <T : Comparable<T>> Sequence<T>.sorted(): Sequence<T>
fun <T> Sequence<T>.sorted(comparator: Comparator<in T>): Sequence<T>
Returns:
- A sequence with the elements sorted in natural order or according to the specified comparator.
Understanding sorted
The sorted function takes a sequence and sorts its elements either in natural order or using a custom comparator. 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 sorted, we will create a sequence of integers and sort them in natural order.
Example
fun main() {
val numbers = sequenceOf(5, 3, 1, 4, 2)
val sortedNumbers = numbers.sorted()
println(sortedNumbers.toList()) // Output: [1, 2, 3, 4, 5]
}
Output:
[1, 2, 3, 4, 5]
Sorting Strings
This example shows how to sort a sequence of strings.
Example
fun main() {
val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha")
val sortedNames = names.sorted()
println(sortedNames.toList()) // Output: [Arjun, Bhaskar, Chitra, Deepak, Esha]
}
Output:
[Arjun, Bhaskar, Chitra, Deepak, Esha]
Using sorted with Custom Comparators
You can use custom comparators with the sorted function to sort elements based on specific criteria.
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 sortedByAge = people.sortedBy { it.age }
println(sortedByAge.toList()) // Output: [Person(name=Chitra, age=22), Person(name=Arjun, age=25), Person(name=Esha, age=26), Person(name=Deepak, age=28), Person(name=Bhaskar, age=30)]
}
Output:
[Person(name=Chitra, age=22), Person(name=Arjun, age=25), Person(name=Esha, age=26), Person(name=Deepak, age=28), Person(name=Bhaskar, age=30)]
Chaining sorted with Other Functions
The sorted 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 }
.sorted()
.map { it * 2 }
println(result.toList()) // Output: [4, 8]
}
Output:
[4, 8]
Real-World Use Case
Sorting Products by Price
In real-world applications, the sorted function can be used to sort products by price, name, or other attributes.
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 sortedByPrice = products.sortedBy { it.price }
println(sortedByPrice.toList()) // Output: [Product(name=Smartwatch, price=199.99), Product(name=Tablet, price=299.99), Product(name=Smartphone, price=499.99), Product(name=Laptop, price=999.99)]
}
Output:
[Product(name=Smartwatch, price=199.99), Product(name=Tablet, price=299.99), Product(name=Smartphone, price=499.99), Product(name=Laptop, price=999.99)]
Conclusion
The sorted function in Kotlin provides used for sorting elements in a sequence. By understanding and using the sorted function, you can efficiently organize data in your Kotlin applications, making them more flexible and responsive to user needs.