minOfWith

inline fun <T, R> Array<out T>.minOfWith(comparator: Comparator<in R>, selector: (T) -> R): R(source)

Returns the smallest value according to the provided comparator among all values produced by selector function applied to each element in the array.

If multiple elements produce the minimal value, this function returns the first of those values.

Since Kotlin

1.4

Throws

if the array is empty.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   data class Book(val title: String, val publishYear: Int, val rating: Double)

// A custom Comparator that orders strings by their length
val lengthComparator = compareBy<String> { it.length }

// The longest and shortest book titles
val books = listOf(
    Book("Red Sand", 2004, 3.5),
    Book("Silver Bullet", 2009, 4.4),
    Book("Clear Water", 2018, 4.1),
    Book("Night Sky", 2023, 3.8)
)
println(books.maxOfWith(lengthComparator) { it.title }) // Silver Bullet
println(books.minOfWith(lengthComparator) { it.title }) // Red Sand

val emptyList = listOf<Book>()

// maxOfWith() and minOfWith() throw if the collection is empty
// emptyList.maxOfWith(lengthComparator) { it.title } // will fail with NoSuchElementException
// emptyList.minOfWith(lengthComparator) { it.title } // will fail with NoSuchElementException

// maxOfWithOrNull() and minOfWithOrNull() return null if the collection is empty
println(emptyList.maxOfWithOrNull(lengthComparator) { it.title }) // null
println(emptyList.minOfWithOrNull(lengthComparator) { it.title }) // null 
   //sampleEnd
}

inline fun <R> ByteArray.minOfWith(comparator: Comparator<in R>, selector: (Byte) -> R): R(source)
inline fun <R> ShortArray.minOfWith(comparator: Comparator<in R>, selector: (Short) -> R): R(source)
inline fun <R> IntArray.minOfWith(comparator: Comparator<in R>, selector: (Int) -> R): R(source)
inline fun <R> LongArray.minOfWith(comparator: Comparator<in R>, selector: (Long) -> R): R(source)
inline fun <R> FloatArray.minOfWith(comparator: Comparator<in R>, selector: (Float) -> R): R(source)
inline fun <R> DoubleArray.minOfWith(comparator: Comparator<in R>, selector: (Double) -> R): R(source)
inline fun <R> BooleanArray.minOfWith(comparator: Comparator<in R>, selector: (Boolean) -> R): R(source)
inline fun <R> CharArray.minOfWith(comparator: Comparator<in R>, selector: (Char) -> R): R(source)
inline fun <R> UIntArray.minOfWith(comparator: Comparator<in R>, selector: (UInt) -> R): R(source)
inline fun <R> ULongArray.minOfWith(comparator: Comparator<in R>, selector: (ULong) -> R): R(source)
inline fun <R> UByteArray.minOfWith(comparator: Comparator<in R>, selector: (UByte) -> R): R(source)
inline fun <R> UShortArray.minOfWith(comparator: Comparator<in R>, selector: (UShort) -> R): R(source)

Returns the smallest value according to the provided comparator among all values produced by selector function applied to each element in the array.

If multiple elements produce the minimal value, this function returns the first of those values.

Since Kotlin

1.4

Throws

if the array is empty.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // A custom Comparator that orders numbers by their absolute values
val absComparator = compareBy<Int> { it.absoluteValue }

// The largest and smallest cubic values when compared by absolute value
val numbers = intArrayOf(-2, 3, -4, 1)
println(numbers.maxOfWith(absComparator) { it * it * it }) // -64
println(numbers.minOfWith(absComparator) { it * it * it }) // 1

val emptyArray = intArrayOf()

// maxOfWith() and minOfWith() throw if the array is empty
// emptyArray.maxOfWith(absComparator) { it * it * it } // will fail with NoSuchElementException
// emptyArray.minOfWith(absComparator) { it * it * it } // will fail with NoSuchElementException

// maxOfWithOrNull() and minOfWithOrNull() return null if the array is empty
println(emptyArray.maxOfWithOrNull(absComparator) { it * it * it }) // null
println(emptyArray.minOfWithOrNull(absComparator) { it * it * it }) // null 
   //sampleEnd
}

inline fun <T, R> Iterable<T>.minOfWith(comparator: Comparator<in R>, selector: (T) -> R): R(source)

Returns the smallest value according to the provided comparator among all values produced by selector function applied to each element in the collection.

If multiple elements produce the minimal value, this function returns the first of those values.

Since Kotlin

1.4

Throws

if the collection is empty.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   data class Book(val title: String, val publishYear: Int, val rating: Double)

// A custom Comparator that orders strings by their length
val lengthComparator = compareBy<String> { it.length }

// The longest and shortest book titles
val books = listOf(
    Book("Red Sand", 2004, 3.5),
    Book("Silver Bullet", 2009, 4.4),
    Book("Clear Water", 2018, 4.1),
    Book("Night Sky", 2023, 3.8)
)
println(books.maxOfWith(lengthComparator) { it.title }) // Silver Bullet
println(books.minOfWith(lengthComparator) { it.title }) // Red Sand

val emptyList = listOf<Book>()

// maxOfWith() and minOfWith() throw if the collection is empty
// emptyList.maxOfWith(lengthComparator) { it.title } // will fail with NoSuchElementException
// emptyList.minOfWith(lengthComparator) { it.title } // will fail with NoSuchElementException

// maxOfWithOrNull() and minOfWithOrNull() return null if the collection is empty
println(emptyList.maxOfWithOrNull(lengthComparator) { it.title }) // null
println(emptyList.minOfWithOrNull(lengthComparator) { it.title }) // null 
   //sampleEnd
}

inline fun <K, V, R> Map<out K, V>.minOfWith(comparator: Comparator<in R>, selector: (Map.Entry<K, V>) -> R): R(source)

Returns the smallest value according to the provided comparator among all values produced by selector function applied to each entry in the map.

If multiple entries produce the minimal value, this function returns the first of those values.

Since Kotlin

1.4

Throws

if the map is empty.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   data class Book(val title: String, val publishYear: Int, val rating: Double)

// A custom Comparator that orders strings by their length
val lengthComparator = compareBy<String> { it.length }

// The longest and shortest book titles
val books = listOf(
    Book("Red Sand", 2004, 3.5),
    Book("Silver Bullet", 2009, 4.4),
    Book("Clear Water", 2018, 4.1),
    Book("Night Sky", 2023, 3.8)
)
println(books.maxOfWith(lengthComparator) { it.title }) // Silver Bullet
println(books.minOfWith(lengthComparator) { it.title }) // Red Sand

val emptyList = listOf<Book>()

// maxOfWith() and minOfWith() throw if the collection is empty
// emptyList.maxOfWith(lengthComparator) { it.title } // will fail with NoSuchElementException
// emptyList.minOfWith(lengthComparator) { it.title } // will fail with NoSuchElementException

// maxOfWithOrNull() and minOfWithOrNull() return null if the collection is empty
println(emptyList.maxOfWithOrNull(lengthComparator) { it.title }) // null
println(emptyList.minOfWithOrNull(lengthComparator) { it.title }) // null 
   //sampleEnd
}