Spec-Zone.ru › Kotlin 2

sortBy

kotlin-stdlib/kotlin.collections/sortBy

Начиная с Kotlin: 1.0

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

Сортирует элементы массива на месте в соответствии с естественным порядком сортировки значений, возвращаемых указанной функцией selector.

Сортировка является стабильной. Это означает, что после сортировки элементы, для которых selector вернула равные значения, сохраняют взаимный порядок.

Примеры

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?)

Сортирует элементы списка на месте в соответствии с естественным порядком сортировки значений, возвращаемых указанной функцией selector.

Сортировка является стабильной. Это означает, что после сортировки элементы, для которых selector вернула равные значения, сохраняют взаимный порядок.

Примеры

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
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/sort-by.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API