sortedByDescending

inline fun <T, R : Comparable<R>> Array<out T>.sortedByDescending(crossinline selector: (T) -> R?): List<T>(source)
inline fun <T, R : Comparable<R>> Iterable<T>.sortedByDescending(crossinline selector: (T) -> R?): List<T>(source)

Returns a list of all elements sorted descending according to natural sort order of the value returned by specified selector function.

The sort is stable. It means that elements for which selector returned equal values preserve their order relative to each other after sorting.

Since Kotlin

1.0

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   class Dish(val name: String, val calories: Int, val tasteRate: Float) {
    override fun toString(): String = "Dish($name: $calories cal, taste $tasteRate/5)"
}

val fridgeContent = listOf(Dish("πŸ₯¦", 34, 2.3f), Dish("πŸ§ƒ", 34, 4.9f), Dish("🍨", 207, 4.7f))

val tastyDishes = fridgeContent.sortedByDescending { it.tasteRate }
println(tastyDishes) // [Dish(πŸ§ƒ: 34 cal, taste 4.9/5), Dish(🍨: 207 cal, taste 4.7/5), Dish(πŸ₯¦: 34 cal, taste 2.3/5)]

val energeticDishes = fridgeContent.sortedByDescending { it.calories }
// note that the sorting is stable, so the πŸ₯¦ is before the πŸ§ƒ
println(energeticDishes) // [Dish(🍨: 207 cal, taste 4.7/5), Dish(πŸ₯¦: 34 cal, taste 2.3/5), Dish(πŸ§ƒ: 34 cal, taste 4.9/5)]

// the original collection remains unchanged
println(fridgeContent) // [Dish(πŸ₯¦: 34 cal, taste 2.3/5), Dish(πŸ§ƒ: 34 cal, taste 4.9/5), Dish(🍨: 207 cal, taste 4.7/5)] 
   //sampleEnd
}

inline fun <R : Comparable<R>> ByteArray.sortedByDescending(crossinline selector: (Byte) -> R?): List<Byte>(source)
inline fun <R : Comparable<R>> ShortArray.sortedByDescending(crossinline selector: (Short) -> R?): List<Short>(source)
inline fun <R : Comparable<R>> IntArray.sortedByDescending(crossinline selector: (Int) -> R?): List<Int>(source)
inline fun <R : Comparable<R>> LongArray.sortedByDescending(crossinline selector: (Long) -> R?): List<Long>(source)
inline fun <R : Comparable<R>> FloatArray.sortedByDescending(crossinline selector: (Float) -> R?): List<Float>(source)
inline fun <R : Comparable<R>> DoubleArray.sortedByDescending(crossinline selector: (Double) -> R?): List<Double>(source)
inline fun <R : Comparable<R>> BooleanArray.sortedByDescending(crossinline selector: (Boolean) -> R?): List<Boolean>(source)
inline fun <R : Comparable<R>> CharArray.sortedByDescending(crossinline selector: (Char) -> R?): List<Char>(source)

Returns a list of all elements sorted descending according to natural sort order of the value returned by specified selector function.

The sort is stable. It means that elements for which selector returned equal values preserve their order relative to each other after sorting.

Since Kotlin

1.0

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val unsorted = intArrayOf(3, 1, 2, 4)

val sortedByValue = unsorted.sortedByDescending { it % 3 }
// the sorting is stable: the order of 1 (1 % 3 == 1) and 4 (4 % 3 == 1) was preserved
println(sortedByValue) // [2, 1, 4, 3]

val mapping = mapOf(1 to "one", 2 to "two", 3 to "three", 4 to "four")
val sortedByPronunciation = unsorted.sortedByDescending {
    // take a key to sort by from the mapping
    mapping.getOrDefault(it, "unknown")
}
// two > three > one > four, lexicographically
println(sortedByPronunciation) // [2, 3, 1, 4]

// the original array remains unchanged
println(unsorted.toList()) // [3, 1, 2, 4] 
   //sampleEnd
}