sortByDescending
inline fun <T, R : Comparable<R>> Array<out T>.sortByDescending(crossinline selector: (T) -> R?)(source)
Sorts elements in the array in-place 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.0Samples
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 = mutableListOf(
Dish("π¨", 207, 4.7f),
Dish("π₯¦", 34, 2.3f),
Dish("π§", 34, 4.9f)
)
// original order
println(fridgeContent) // [Dish(π¨: 207 cal, taste 4.7/5), Dish(π₯¦: 34 cal, taste 2.3/5), Dish(π§: 34 cal, taste 4.9/5)]
// sort by taste rate (descending)
fridgeContent.sortByDescending { it.tasteRate }
println(fridgeContent) // [Dish(π§: 34 cal, taste 4.9/5), Dish(π¨: 207 cal, taste 4.7/5), Dish(π₯¦: 34 cal, taste 2.3/5)]
val breadBoxContent = mutableListOf(
Dish("π₯―", 245, 4.8f),
Dish("π₯¨", 100, 5.0f),
Dish("π₯", 245, 4.9f)
)
// original order
println(breadBoxContent) // [Dish(π₯―: 245 cal, taste 4.8/5), Dish(π₯¨: 100 cal, taste 5.0/5), Dish(π₯: 245 cal, taste 4.9/5)]
// sort by calories (descending)
breadBoxContent.sortByDescending { it.calories }
// note that the sorting is stable, so the π₯― is before the π₯
println(breadBoxContent) // [Dish(π₯―: 245 cal, taste 4.8/5), Dish(π₯: 245 cal, taste 4.9/5), Dish(π₯¨: 100 cal, taste 5.0/5)]
//sampleEnd
}inline fun <T, R : Comparable<R>> MutableList<T>.sortByDescending(crossinline selector: (T) -> R?)(source)
Sorts elements in the list in-place 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.0Samples
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 = mutableListOf(
Dish("π¨", 207, 4.7f),
Dish("π₯¦", 34, 2.3f),
Dish("π§", 34, 4.9f)
)
// original order
println(fridgeContent) // [Dish(π¨: 207 cal, taste 4.7/5), Dish(π₯¦: 34 cal, taste 2.3/5), Dish(π§: 34 cal, taste 4.9/5)]
// sort by taste rate (descending)
fridgeContent.sortByDescending { it.tasteRate }
println(fridgeContent) // [Dish(π§: 34 cal, taste 4.9/5), Dish(π¨: 207 cal, taste 4.7/5), Dish(π₯¦: 34 cal, taste 2.3/5)]
val breadBoxContent = mutableListOf(
Dish("π₯―", 245, 4.8f),
Dish("π₯¨", 100, 5.0f),
Dish("π₯", 245, 4.9f)
)
// original order
println(breadBoxContent) // [Dish(π₯―: 245 cal, taste 4.8/5), Dish(π₯¨: 100 cal, taste 5.0/5), Dish(π₯: 245 cal, taste 4.9/5)]
// sort by calories (descending)
breadBoxContent.sortByDescending { it.calories }
// note that the sorting is stable, so the π₯― is before the π₯
println(breadBoxContent) // [Dish(π₯―: 245 cal, taste 4.8/5), Dish(π₯: 245 cal, taste 4.9/5), Dish(π₯¨: 100 cal, taste 5.0/5)]
//sampleEnd
}