Instance Method インスタンスメソッド

min(by:)

Returns the minimum element in the sequence, using the given predicate as the comparison between elements. シーケンスの中の最小の要素を返します、与えられた述部を要素間の比較として使います。

Declaration 宣言

@warn_unqualified_access func min(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> Element?

Parameters パラメータ

areInIncreasingOrder

A predicate that returns true if its first argument should be ordered before its second argument; otherwise, false. ある述部、それはそれの最初の引数がそれの2番目の引数の前に並べられるべきならばtrueを返します;そうでなければ、false

Return Value 戻り値

The sequence’s minimum element, according to areInIncreasingOrder. If the sequence has no elements, returns nil. シーケンスの最小の要素、areInIncreasingOrderによる。シーケンスが要素を持たないならば、nilを返します。

Discussion 解説

The predicate must be a strict weak ordering over the elements. That is, for any elements a, b, and c, the following conditions must hold: 述部は、それら要素に対して厳密弱順序でなければなりません。すなわち、何らかの要素ab、そしてcに対して、以下の条件が保持されなければなりません:

  • areInIncreasingOrder(a, a) is always false. (Irreflexivity) areInIncreasingOrder(a, a)は常にfalseである。(非反射)

  • If areInIncreasingOrder(a, b) and areInIncreasingOrder(b, c) are both true, then areInIncreasingOrder(a, c) is also true. (Transitive comparability) areInIncreasingOrder(a, b)areInIncreasingOrder(b, c)が両方ともtrueならば、そのときareInIncreasingOrder(a, c)もまたtrueである。(推移的比較性)

  • Two elements are incomparable if neither is ordered before the other according to the predicate. If a and b are incomparable, and b and c are incomparable, then a and c are also incomparable. (Transitive incomparability) 2つの要素は、述部によるとどちらもが他の前に並べられるならば比較できないabが比較できないならば、そしてbcが比較できないならば、そのときacもまた比較できない。(推移的比較不能性)

This example shows how to use the min(by:) method on a dictionary to find the key-value pair with the lowest value. この例は、min(by:)メソッドを辞書上で使って、最も低い値を持つキー値ペアを見つける方法を示します。


let hues = ["Heliotrope": 296, "Coral": 16, "Aquamarine": 156]
let leastHue = hues.min { a, b in a.value < b.value }
print(leastHue)
// Prints "Optional((key: "Coral", value: 16))"

Complexity: O(n), where n is the length of the sequence. 計算量:O(n)、ここでnはシーケンスの長さです。

See Also 参照

Finding Elements 要素を見つける