---
schema_version: "1"
id: d2310a85f02e8574e1defad681d72324be2af0c5b964d3a48f05fc1807477bd2
title: minBy
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.3/dw-core-functions-minby.md
canonical_url: https://docs.mulesoft.com/dataweave/2.3/dw-core-functions-minby
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 049e2e5a2ea10aef8dd695b277952da7d10b7cf819b67385cca0cd1690757e5c
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: DataWeave
version: 2.3
---

# minBy

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

## minBy(Array<T>, (item: T) -> Comparable): T | Null

Iterates over an array to return the lowest value of comparable elements from it.

The items need to be of the same type. `minBy` returns an error if they are not, and it returns null
when 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>item</code></p></td><td><p>Element in the input array (of type <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>). Can be referenced with <code>$</code>.</p></td></tr></tbody></table>

### Example

This example returns the lowest 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 } ] minBy $.a`

#### Source

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

#### Output

```JSON
{ "a" : 1 }
```

### Example

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
---
{
  myMinBy: {
    byDateTime: [ myDateTime1, myDateTime2 ] minBy ((item) -> item),
    byDate: [ myDateTime1 as Date, myDateTime2 as Date ] minBy ((item) -> item),
    byTime: [ myDateTime1 as Time, myDateTime2 as Time ] minBy ((item) -> item),
    aBoolean: [ true, false, (0 > 1), (1 > 0) ] minBy $,
    emptyArray: [] minBy ((item) -> item)
  }
}
```

#### Output

```JSON
{
  "myMinBy": {
    "byDateTime": "2017-10-01T22:57:59-03:00",
    "byDate": "2017-10-01",
    "byTime": "22:57:59-03:00",
    "aBoolean": false,
    "emptyArray": null
  }
}
```
