sortBy

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

Sorts elements in the array in-place 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 = 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 (ascending)
fridgeContent.sortBy { it.tasteRate }
println(fridgeContent) // [Dish(πŸ₯¦: 34 cal, taste 2.3/5), Dish(🍨: 207 cal, taste 4.7/5), Dish(πŸ§ƒ: 34 cal, taste 4.9/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 (ascending)
breadBoxContent.sortBy { it.calories }
// note that the sorting is stable, so the πŸ₯― is before the πŸ₯
println(breadBoxContent) // [Dish(πŸ₯¨: 100 cal, taste 5.0/5), Dish(πŸ₯―: 245 cal, taste 4.8/5), Dish(πŸ₯: 245 cal, taste 4.9/5)] 
   //sampleEnd
}

inline fun <T, R : Comparable<R>> MutableList<T>.sortBy(crossinline selector: (T) -> R?)(source)

Sorts elements in the list in-place 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 = 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 (ascending)
fridgeContent.sortBy { it.tasteRate }
println(fridgeContent) // [Dish(πŸ₯¦: 34 cal, taste 2.3/5), Dish(🍨: 207 cal, taste 4.7/5), Dish(πŸ§ƒ: 34 cal, taste 4.9/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 (ascending)
breadBoxContent.sortBy { it.calories }
// note that the sorting is stable, so the πŸ₯― is before the πŸ₯
println(breadBoxContent) // [Dish(πŸ₯¨: 100 cal, taste 5.0/5), Dish(πŸ₯―: 245 cal, taste 4.8/5), Dish(πŸ₯: 245 cal, taste 4.9/5)] 
   //sampleEnd
}