W3cubDocs

/Kotlin

minByOrNull

Platform and version requirements: JVM (1.4), JS (1.4), Native (1.4)
inline fun <T, R : Comparable<R>> Array<out T>.minByOrNull(
    selector: (T) -> R
): T?
inline fun <R : Comparable<R>> ByteArray.minByOrNull(
    selector: (Byte) -> R
): Byte?
inline fun <R : Comparable<R>> ShortArray.minByOrNull(
    selector: (Short) -> R
): Short?
inline fun <R : Comparable<R>> IntArray.minByOrNull(
    selector: (Int) -> R
): Int?
inline fun <R : Comparable<R>> LongArray.minByOrNull(
    selector: (Long) -> R
): Long?
inline fun <R : Comparable<R>> FloatArray.minByOrNull(
    selector: (Float) -> R
): Float?
inline fun <R : Comparable<R>> DoubleArray.minByOrNull(
    selector: (Double) -> R
): Double?
inline fun <R : Comparable<R>> BooleanArray.minByOrNull(
    selector: (Boolean) -> R
): Boolean?
inline fun <R : Comparable<R>> CharArray.minByOrNull(
    selector: (Char) -> R
): Char?
inline fun <T, R : Comparable<R>> Iterable<T>.minByOrNull(
    selector: (T) -> R
): T?
@ExperimentalUnsignedTypes inline fun <R : Comparable<R>> UIntArray.minByOrNull(
    selector: (UInt) -> R
): UInt?
@ExperimentalUnsignedTypes inline fun <R : Comparable<R>> ULongArray.minByOrNull(
    selector: (ULong) -> R
): ULong?
@ExperimentalUnsignedTypes inline fun <R : Comparable<R>> UByteArray.minByOrNull(
    selector: (UByte) -> R
): UByte?
@ExperimentalUnsignedTypes inline fun <R : Comparable<R>> UShortArray.minByOrNull(
    selector: (UShort) -> R
): UShort?

Returns the first element yielding the smallest value of the given function or null if there are no elements.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf("abcd", "abc", "ab", "abcde")
val shortestString = list.minByOrNull { it.length }
println(shortestString) // ab

val emptyList = emptyList<String>()
val emptyMin = emptyList.minByOrNull { it.length }
println(emptyMin) // null
//sampleEnd
}
Platform and version requirements: JVM (1.4), JS (1.4), Native (1.4)
inline fun <K, V, R : Comparable<R>> Map<out K, V>.minByOrNull(
    selector: (Entry<K, V>) -> R
): Entry<K, V>?

Returns the first entry yielding the smallest value of the given function or null if there are no entries.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf("abcd", "abc", "ab", "abcde")
val shortestString = list.minByOrNull { it.length }
println(shortestString) // ab

val emptyList = emptyList<String>()
val emptyMin = emptyList.minByOrNull { it.length }
println(emptyMin) // null
//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.collections/min-by-or-null.html