Kotlin Sequence sortedBy Function

The sortedBy function in Kotlin is used to sort the elements in a sequence based on a specified selector function. It is part of the Kotlin standard library and allows you to sort elements according to custom criteria.

Table of Contents

  1. Introduction
  2. sortedBy Function Syntax
  3. Understanding sortedBy
  4. Examples
    • Basic Usage
    • Sorting Strings by Length
    • Using sortedBy with Custom Objects
    • Chaining sortedBy with Other Functions
  5. Real-World Use Case
  6. Conclusion

Introduction

The sortedBy function allows you to sort the elements of a sequence based on a custom selector function. This is useful for scenarios where you need to arrange elements according to specific criteria, enabling more precise data manipulation and presentation.

sortedBy Function Syntax

The syntax for the sortedBy function is as follows:

fun <T, R : Comparable<R>> Sequence<T>.sortedBy(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 based on the specified criteria.

Understanding sortedBy

The sortedBy function takes a sequence and sorts its elements according to the value returned by the selector function for each element. 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 sortedBy, we will create a sequence of integers and sort them by their absolute values.

Example

fun main() { val numbers = sequenceOf(-3, 2, -5, 4, -1) val sortedNumbers = numbers.sortedBy { it.absoluteValue } println(sortedNumbers.toList()) // Output: [-1, 2, -3, 4, -5] } 

Output:

[-1, 2, -3, 4, -5] 

Sorting Strings by Length

This example shows how to sort a sequence of strings by their length.

Example

fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha") val sortedNames = names.sortedBy { it.length } println(sortedNames.toList()) // Output: [Esha, Arjun, Chitra, Bhaskar, Deepak] } 

Output:

[Esha, Arjun, Chitra, Bhaskar, Deepak] 

Using sortedBy with Custom Objects

You can use the sortedBy function to sort custom objects based on specific properties.

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 sortedBy with Other Functions

The sortedBy 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 } .sortedBy { it } .map { it * 2 } println(result.toList()) // Output: [4, 8] } 

Output:

[4, 8] 

Real-World Use Case

Sorting Products by Name

In real-world applications, the sortedBy function can be used to sort products by name, price, 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 sortedByName = products.sortedBy { it.name } println(sortedByName.toList()) // Output: [Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99), Product(name=Smartwatch, price=199.99), Product(name=Tablet, price=299.99)] } 

Output:

[Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99), Product(name=Smartwatch, price=199.99), Product(name=Tablet, price=299.99)] 

Conclusion

The sortedBy function in Kotlin provides used for sorting elements in a sequence based on custom criteria. By understanding and using the sortedBy function, you can efficiently organize data in your Kotlin applications, making them more flexible and responsive to user needs.

Leave a Comment

Scroll to Top