---
schema_version: "1"
id: 95e8dfeb45628f3aadebde080efc6b06020e04b22c9c566514af3d0494568e9e
title: maxBy
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.9/dw-core-functions-maxby.md
canonical_url: https://docs.mulesoft.com/dataweave/2.9/dw-core-functions-maxby
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 33d1466030f130876934a0b60275b39932a69201129fbc78b55d1b1c4dedd0df
description: MuleSoft Documentation Site
release: "2.9"
latest: false
---
---
product: DataWeave
version: 2.9
---

# maxBy

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## maxBy<T>(@StreamCapable array: Array<T>, criteria: (item: T) -> Comparable): T | Null

Iterates over an array and returns the highest value of `Comparable` elements from it.

The items must be of the same type. `maxBy` throws an error if they are not, and the function
returns `null` if the array is empty.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>array</code></p></td><td><p>The input array.</p></td></tr><tr><td><p><code>criteria</code></p></td><td><p>Expression for selecting an item from the array, where the item is a <code>Number</code>, <code>Boolean</code>, <code>DateTime</code>, <code>LocalDateTime</code>, <code>Date</code>, <code>LocalTime</code>, <code>Time</code>, or <code>TimeZone</code> data type. Can be referenced with <code>$</code>.</p></td></tr></tbody></table>

### Example: Numeric Values

This example returns the greatest numeric value within objects (key-value pairs) in an array. Notice
that it uses `item.a` to select the value of the object. You can also write the same expression like
this, using an anonymous parameter: `[ { "a" : 1 }, { "a" : 3 }, { "a" : 2 } ] maxBy $.a`

#### Source

```DataWeave
%dw 2.0
output  application/json
---
[ { "a" : 1 }, { "a" : 3 }, { "a" : 2 } ] maxBy ((item) -> item.a)
```

#### Output

```JSON
{ "a" : 3 }
```

### Example: Date and Time Values

This example gets the latest `DateTime`, `Date`, and `Time` from inputs defined in the variables
`myDateTime1` and `myDateTime2`. It also shows that the function returns null on an empty array.

#### Source

```DataWeave
%dw 2.0
var myDateTime1 = |2017-10-01T22:57:59-03:00|
var myDateTime2 = |2018-10-01T23:57:59-03:00|
output application/json
---
{
  myMaxBy: {
    byDateTime: [ myDateTime1, myDateTime2 ] maxBy ((item) -> item),
    byDate: [ myDateTime1 as Date, myDateTime2 as Date ] maxBy ((item) -> item),
    byTime: [ myDateTime1 as Time, myDateTime2 as Time ] maxBy ((item) -> item),
    emptyArray: [] maxBy ((item) -> item)
  }
}
```

#### Output

```JSON
{
  "myMaxBy": {
    "byDateTime": "2018-10-01T23:57:59-03:00",
    "byDate": "2018-10-01",
    "byTime": "23:57:59-03:00",
    "emptyArray": null
  }
}
```

### Example: String Values

`maxBy` can also compare strings. The following example demonstrates this using simple string
elements.

#### Source

```DataWeave
[ "A", "B", "C" ] maxBy ((item) -> item)
```

#### Output

```JSON
"C"
```
