Files
lo/docs/data/it-minindexby.md
T
2025-10-08 19:35:53 +02:00

1.2 KiB

name, slug, sourceRef, category, subCategory, signatures, playUrl, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures playUrl variantHelpers similarHelpers position
MinIndexBy minindexby it/find.go#L258 it find
func MinIndexBy[T any](collection iter.Seq[T], comparison func(a, b T) bool) (T, int)
https://go.dev/play/p/6DxQUQ0-zc
it#find#minindexby
core#slice#minindexby
470

Searches the minimum value of a collection using a comparison function and returns both the value and its index.

If several values are equal to the smallest value, returns the first such value. Returns (zero value, -1) when the collection is empty. Will iterate through the entire sequence.

Examples:

// Find the minimum string by length and its index
words := it.Slice([]string{"apple", "hi", "banana", "ok"})
value, index := it.MinIndexBy(words, func(a, b string) bool {
    return len(a) < len(b)
})
// value: "hi", index: 1

// Find the minimum person by age and its index
people := it.Slice([]Person{
    {Name: "Alice", Age: 30},
    {Name: "Bob", Age: 25},
    {Name: "Charlie", Age: 35},
})
value, index := it.MinIndexBy(people, func(a, b Person) bool {
    return a.Age < b.Age
})
// value: {Name: "Bob", Age: 25}, index: 1