The documentation of removeAll(where:)
says nothing about order, although the example in the discussion section of the documentation indicates that it does preserve the order:
Use this method to remove every element in a collection that meets particular criteria. This example removes all the odd values from an array of numbers:
var numbers = [5, 6, 7, 8, 9, 10, 11] numbers.removeAll(where: { $0 % 2 == 1 }) // numbers == [6, 8, 10]
Complexity: O(n), where n is the length of the collection.
So maybe the doc should say something about order, similar to eg filter(isIncluded:)
:
Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.
?
PS
Also note that the example code in the documentation is making the common mistake of writing a predicate for odd as $0 % 2 == 1
rather than $0 % 2 != 0
, which means that it will not remove any negative numbers:
var numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
numbers.removeAll(where: { $0 % 2 == 1 })
// numbers = [-5, -4, -3, -2, -1, 0 2 4]
This is better:
var numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
numbers.removeAll(where: { $0 % 2 != 0 })
// numbers = [-4, -2, 0 2 4]