min(count:sortedBy:)

Returns the smallest elements of this sequence, as sorted by the given predicate.

MinMax.swift:104
func min(count: Int, sortedBy areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> [Element]

Parameters

count

The number of elements to return. If count is greater than the number of elements in this sequence, all of the sequence’s elements are returned.

areInIncreasingOrder

A predicate that returns true if its first argument should be ordered before its second argument; otherwise, false.

Returns

An array of the smallest count elements of this sequence, sorted according to areInIncreasingOrder.

This example partially sorts an array of integers to retrieve its three smallest values:

let numbers = [7, 1, 6, 2, 8, 3, 9]
let smallestThree = numbers.min(count: 3, sortedBy: <)
// [1, 2, 3]

If you need to sort a sequence but only need to access its smallest elements, using this method can give you a performance boost over sorting the entire sequence. The order of equal elements is guaranteed to be preserved.