W3cubDocs

/Kotlin

naturalOrder

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T : Comparable<T>> naturalOrder(): Comparator<T>

Returns a comparator that compares Comparable objects in natural order.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf("aa", "b", "bb", "a")

val lengthThenNatural = compareBy<String> { it.length }
    .then(naturalOrder())

val sorted = list.sortedWith(lengthThenNatural)

println(sorted) // [a, b, aa, bb]
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.comparisons/natural-order.html