minByOrNull

inline fun <T, R : Comparable<R>> Array<out T>.minByOrNull(selector: (T) -> R): T?(source)
inline fun <R : Comparable<R>> ByteArray.minByOrNull(selector: (Byte) -> R): Byte?(source)
inline fun <R : Comparable<R>> ShortArray.minByOrNull(selector: (Short) -> R): Short?(source)
inline fun <R : Comparable<R>> IntArray.minByOrNull(selector: (Int) -> R): Int?(source)
inline fun <R : Comparable<R>> LongArray.minByOrNull(selector: (Long) -> R): Long?(source)
inline fun <R : Comparable<R>> FloatArray.minByOrNull(selector: (Float) -> R): Float?(source)
inline fun <R : Comparable<R>> DoubleArray.minByOrNull(selector: (Double) -> R): Double?(source)
inline fun <R : Comparable<R>> BooleanArray.minByOrNull(selector: (Boolean) -> R): Boolean?(source)
inline fun <R : Comparable<R>> CharArray.minByOrNull(selector: (Char) -> R): Char?(source)
inline fun <T, R : Comparable<R>> Iterable<T>.minByOrNull(selector: (T) -> R): T?(source)

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

Since Kotlin

1.4

Samples

import kotlin.test.*

fun main() { 
   //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
}

inline fun <K, V, R : Comparable<R>> Map<out K, V>.minByOrNull(selector: (Map.Entry<K, V>) -> R): Map.Entry<K, V>?(source)

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

Since Kotlin

1.4

Samples

import kotlin.test.*

fun main() { 
   //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
}