# maxBy

## definition

```scala
def maxBy[B](f: (A) ⇒ B): A
```

## demo

```scala
case class Donut(name: String, price: Double)

val donuts: Seq[Donut] = Seq(Donut("Plain Donut", 1.5), Donut("Strawberry Donut", 2.0), Donut("Glazed Donut", 2.5))

// find the maximum element in a sequence of case classes objects using the maxBy function
val max1 = donuts.maxBy(donut => donut.price)


// declare a value predicate function for maxBy function
val donutsMaxBy: (Donut) => Double = (donut) => donut.price
val max2 = donuts.maxBy(donutsMaxBy)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.selinux.tech/scala/function-operation/m/maxby.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
