Back to Lo

Core Maxindexby

docs/data/core-maxindexby.md

1.53.0459 B
Original Source

Returns the maximum value and its index using the given comparison function. Returns (zero value, -1) when the collection is empty.

go
type Point struct{ X int }
value, idx := lo.MaxIndexBy([]Point{{1}, {5}, {3}}, func(a, b Point) bool {
    return a.X > b.X
})
// value == {5}, idx == 1

Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention.

See https://github.com/samber/lo/issues/129