Files
lo/docs/data/core-maxindexby.md
T
Samuel Berthe fa095e4b4f fix(doc): fix go playground demo URL (#832)
* fix(doc): fix go playground demo URL

* fix(doc): add more go playground demo URL
2026-03-06 00:09:59 +01:00

1.0 KiB

name, slug, sourceRef, category, subCategory, playUrl, variantHelpers, similarHelpers, position, signatures
name slug sourceRef category subCategory playUrl variantHelpers similarHelpers position signatures
MaxIndexBy maxindexby find.go#L566 core find https://go.dev/play/p/5yd4W7pe2QJ
core#find#maxindexby
core#find#max
core#find#maxby
core#find#maxbyerr
core#find#maxindex
core#find#maxindexbyerr
core#find#min
core#find#minby
core#find#minindex
core#find#minindexby
core#math#sum
core#math#mean
core#math#product
core#math#mode
230
func MaxIndexBy[T any](collection []T, comparison func(a T, b T) bool) (T, int)

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

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