---
title: "tryMax(by:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/trymax(by:)"
---

# tryMax(by:)

Publishes the maximum value received from the upstream publisher, using the provided error-throwing closure to order the items.

## Declaration

```swift
func tryMax(by areInIncreasingOrder: @escaping (Self.Output, Self.Output) throws -> Bool) -> Publishers.TryComparison<Self>
```

## Parameters

- `areInIncreasingOrder`: A throwing closure that receives two elements and returns true if they’re in increasing order. If this closure throws, the publisher terminates with a doc://com.apple.Combine/documentation/Combine/Subscribers/Completion/failure(_:).

## Return Value

Return Value A publisher that publishes the maximum value received from the upstream publisher, after the upstream publisher finishes.

## Discussion

Discussion Use tryMax(by:) to determine the maximum value of elements received from the upstream publisher using an error-throwing closure you specify. In the example below, an array publishes elements. The tryMax(by:) operator executes the error-throwing closure that throws when the first element is an odd number, terminating the publisher. struct IllegalValueError: Error {}

let numbers: [Int]  = [0, 10, 6, 13, 22, 22] cancellable = numbers.publisher     .tryMax { first, second -> Bool in         if (first % 2 != 0) {             throw IllegalValueError()         }         return first > second     }     .sink(         receiveCompletion: { print ("completion: \($0)") },         receiveValue: { print ("value: \($0)") }     )

// Prints: completion: failure(IllegalValueError()) After this publisher receives a request for more than 0 items, it requests unlimited items from its upstream publisher.

## See Also

### Applying mathematical operations on elements

- [count()](combine/publisher/count().md)
- [max()](combine/publisher/max().md)
- [max(by:)](combine/publisher/max(by:).md)
- [min()](combine/publisher/min().md)
- [min(by:)](combine/publisher/min(by:).md)
- [tryMin(by:)](combine/publisher/trymin(by:).md)
