Instance Method

partition(by:)

Reorders the elements of the collection such that all the elements that match the given predicate are after all the elements that don’t match.

Parameters

`belongsInSecondPartition`

A predicate used to partition the collection. All elements satisfying this predicate are ordered after all elements not satisfying it.

Return Value

The index of the first element in the reordered collection that matches `belongsInSecondPartition`. If no elements in the collection match `belongsInSecondPartition`, the returned index is equal to the collection’s `endIndex`.

Discussion

After partitioning a collection, there is a pivot index `p` where no element before `p` satisfies the `belongsInSecondPartition` predicate and every element at or after `p` satisfies `belongsInSecondPartition`.

In the following example, an array of numbers is partitioned by a predicate that matches elements greater than 30.

The `numbers` array is now arranged in two partitions. The first partition, `numbers[..<p]`, is made up of the elements that are not greater than 30. The second partition, `numbers[p...]`, is made up of the elements that are greater than 30.

Complexity: O(n), where n is the length of the collection.

Reordering an Array's Elements

`func sort()`

Sorts the collection in place.

`func sort(by: (Element, Element) -> Bool)`

Sorts the collection in place, using the given predicate as the comparison between elements.

`func sorted() -> [Element]`

Returns the elements of the sequence, sorted.

`func sorted(by: (Element, Element) -> Bool) -> [Element]`

Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.

`func reverse()`

Reverses the elements of the collection in place.

`func reversed() -> ReversedCollection<Array<Element>>`

Returns a view presenting the elements of the collection in reverse order.

`func shuffle()`

Shuffles the collection in place.

`func shuffle<T>(using: inout T)`

Shuffles the collection in place, using the given generator as a source for randomness.

`func shuffled() -> [Element]`

Returns the elements of the sequence, shuffled.

`func shuffled<T>(using: inout T) -> [Element]`

Returns the elements of the sequence, shuffled using the given generator as a source for randomness.

`func swapAt(Int, Int)`

Exchanges the values at the specified indices of the collection.