Back to Lo

It Minindexby

docs/data/it-minindexby.md

1.53.0845 B
Original Source

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:

go
// 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