Returns the maximum element in the sequence, using the given predicate as the comparison between elements.
- Xcode 9.0+
- Swift Standard Library
A predicate that returns
trueif its first argument should be ordered before its second argument; otherwise,
The sequence’s maximum element if the sequence is not empty; otherwise,
The predicate must be a strict weak ordering over the elements. That is, for any elements
c, the following conditions must hold:
In Increasing Order(a, a)
In Increasing Order(a, b)
In Increasing Order(b, c)
In Increasing Order(a, c)
true. (Transitive comparability)
Two elements are incomparable if neither is ordered before the other according to the predicate. If
bare incomparable, and
care incomparable, then
care also incomparable. (Transitive incomparability)
This example shows how to use the
max(by:) method on a dictionary to find the key-value pair with the highest value.
Complexity: O(n), where n is the length of the sequence.