# Does removeAll(where:) on arrays guarantee preserved order of elements?

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]
``````
3 Likes

First, to answer the question: yes, it does.

Can you file a bug for the documentation issue? CC @nnnnnnnn.

1 Like

`removeAll(where:)` was introduced as an in-place `filter`, so I am sure that it is meant to preserve the order (and inspecting the source code shows that it does).

However, documenting that behavior explicitly seems appropriate to me.

1 Like

Here's a PR for the preserved order part: https://github.com/apple/swift/pull/18803

3 Likes