Core API – Kotlin Programming Language (original) (raw)

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: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>a</mi><mi>l</mi><mi>o</mi><mi>r</mi><mi>i</mi><mi>e</mi><mi>s</mi><mi>c</mi><mi>a</mi><mi>l</mi><mo separator="true">,</mo><mi>t</mi><mi>a</mi><mi>s</mi><mi>t</mi><mi>e</mi></mrow><annotation encoding="application/x-tex">calories cal, taste </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">c</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">i</span><span class="mord mathnormal">esc</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span></span></span></span>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
}

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: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>a</mi><mi>l</mi><mi>o</mi><mi>r</mi><mi>i</mi><mi>e</mi><mi>s</mi><mi>c</mi><mi>a</mi><mi>l</mi><mo separator="true">,</mo><mi>t</mi><mi>a</mi><mi>s</mi><mi>t</mi><mi>e</mi></mrow><annotation encoding="application/x-tex">calories cal, taste </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">c</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">i</span><span class="mord mathnormal">esc</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span></span></span></span>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
}