Back to Lo

Core Maxby

docs/data/core-maxby.md

1.53.0425 B
Original Source

Searches the maximum value of a collection using the given comparison function. Returns zero value when the collection is empty.

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

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