> For the complete documentation index, see [llms.txt](https://gold.gitbook.io/kotlin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gold.gitbook.io/kotlin/collections/aggregate-operations/maxby.md).

# maxBy

```kotlin
public inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R):T? {
    val iterator = iterator()
    if (!iterator.hasNext()) return null
    var maxElem = iterator.next()
    var maxValue = selector(maxElem)
    while (iterator.hasNext()) {
        val e = iterator.next()
        val v = selector(e)
        if (maxValue < v) {
            maxElem = e
            maxValue = v
        }
    }
    return maxElem
}


val list = listOf(Person("a", 10),
        Person("b", 30),
        Person("c", 15))
        
println(list.maxBy{ it.age })  //Person(name=b, age=30)
```
