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

**URL:** <https://forums.swift.org/t/does-removeall-where-on-arrays-guarantee-preserved-order-of-elements/15313>\
**Category:** Using Swift\
**Created:** [August 17, 2018, 11:49am UTC](https://forums.swift.org/t/does-removeall-where-on-arrays-guarantee-preserved-order-of-elements/15313 "2018-08-17T11:49:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jens](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jens/32/12903_2.png) [@Jens](https://forums.swift.org/u/Jens)\
**Post date:** [August 17, 2018, 11:49am UTC](https://forums.swift.org/t/does-removeall-where-on-arrays-guarantee-preserved-order-of-elements/15313/1 "2018-08-17T11:49:02Z")

</div>

The [documentation](https://developer.apple.com/documentation/swift/array/3017532-removeall) 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:
> 
> ```swift
> 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](https://forums.swift.org/t/even-and-odd-integers/11774/54) 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:

```swift
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:

```swift
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]

```

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [August 17, 2018, 12:28pm UTC](https://forums.swift.org/t/does-removeall-where-on-arrays-guarantee-preserved-order-of-elements/15313/2 "2018-08-17T12:28:42Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forums.swift.org/u/Martin)\
**Post date:** [August 17, 2018, 12:29pm UTC](https://forums.swift.org/t/does-removeall-where-on-arrays-guarantee-preserved-order-of-elements/15313/3 "2018-08-17T12:29:18Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![krilnon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/krilnon/32/276_2.png) [@krilnon](https://forums.swift.org/u/krilnon)\
**Post date:** [August 17, 2018, 8:40pm UTC](https://forums.swift.org/t/does-removeall-where-on-arrays-guarantee-preserved-order-of-elements/15313/4 "2018-08-17T20:40:22Z")

</div>

Here's a PR for the preserved order part: [[stdlib] Document that removeAll(where:) doesn't reorder the remaining elements. by krilnon · Pull Request #18803 · apple/swift · GitHub](https://github.com/apple/swift/pull/18803)
