Kotlin Array sortByDescending Function

The sortByDescending function in Kotlin is used to sort elements in an array in-place based on a specified selector function, in descending order. This function is part of the Kotlin standard library and provides a way to sort arrays according to a custom property or condition from highest to lowest.

Table of Contents

  1. Introduction
  2. sortByDescending Function Syntax
  3. Understanding sortByDescending
  4. Examples
    • Basic Usage
    • Using sortByDescending with Custom Types
    • Sorting with Multiple Criteria
  5. Real-World Use Case
  6. Conclusion

Introduction

The sortByDescending function sorts the elements of an array in-place in descending order according to the value returned by the specified selector function. It is a simple and effective way to sort arrays based on custom criteria from highest to lowest.

sortByDescending Function Syntax

The syntax for the sortByDescending function is as follows:

inline fun <T, R : Comparable<R>> Array<out T>.sortByDescending(selector: (T) -> R?): Unit

Parameters:

  • selector: A lambda function that takes an element of type T and returns a value of type R that is used for sorting.

Returns:

  • This function does not return a value.

Understanding sortByDescending

The sortByDescending function is used to sort elements in an array based on the value returned by the selector function in descending order. This is particularly useful for sorting custom objects or when sorting based on specific properties of the elements.

Examples

Basic Usage

To demonstrate the basic usage of sortByDescending, we will create an array of integers and sort them by their absolute values in descending order.

Example

fun main() {
    val numbers = arrayOf(-3, 2, -1, 4, -5)
    numbers.sortByDescending { it.absoluteValue }
    println("Sorted by absolute value in descending order: ${numbers.joinToString()}")
}

Output:

Sorted by absolute value in descending order: -5, 4, -3, 2, -1

Using sortByDescending with Custom Types

This example shows how to use sortByDescending to sort an array of custom objects based on a specific property.

Example

data class Person(val name: String, val age: Int)

fun main() {
    val people = arrayOf(
        Person("Ravi", 25),
        Person("Anjali", 30),
        Person("Priya", 22)
    )

    people.sortByDescending { it.age }
    println("Sorted people by age in descending order: ${people.joinToString()}")
}

Output:

Sorted people by age in descending order: Person(name='Anjali', age=30), Person(name='Ravi', age=25), Person(name='Priya', age=22)

Sorting with Multiple Criteria

This example demonstrates how to use sortWith and compareByDescending to sort an array based on multiple criteria in descending order.

Example

data class Person(val name: String, val age: Int, val height: Double)

fun main() {
    val people = arrayOf(
        Person("Ravi", 25, 5.8),
        Person("Anjali", 30, 5.5),
        Person("Priya", 22, 5.6),
        Person("Ravi", 22, 5.7)
    )

    people.sortWith(compareByDescending<Person> { it.name }.thenByDescending { it.age })
    println("Sorted people by name and then by age in descending order: ${people.joinToString()}")
}

Output:

Sorted people by name and then by age in descending order: [Person(name='Ravi', age=25, height=5.8), Person(name='Ravi', age=22, height=5.7), Person(name='Priya', age=22, height=5.6), Person(name='Anjali', age=30, height=5.5)]

Real-World Use Case

Sorting Products by Price

In real-world applications, the sortByDescending function can be used to sort data objects based on various properties, such as sorting a list of products by price in descending order.

Example

data class Product(val name: String, val price: Double)

fun main() {
    val products = arrayOf(
        Product("Laptop", 999.99),
        Product("Smartphone", 699.99),
        Product("Tablet", 299.99)
    )

    products.sortByDescending { it.price }
    println("Products sorted by price in descending order: ${products.joinToString()}")
}

Output:

Products sorted by price in descending order: Product(name='Laptop', price=999.99), Product(name='Smartphone', price=699.99), Product(name='Tablet', price=299.99)

Conclusion

The sortByDescending function in Kotlin is used for sorting elements in an array based on a specified property or condition in descending order. It allows you to sort elements in-place based on custom criteria from highest to lowest, making it useful for sorting custom objects and data. By understanding and using this function, you can effectively manage data sorting in your Kotlin applications.

Comments