---
title: "swapAt(_:_:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/array/swapat(_:_:)"
---

# swapAt(_:_:)

Exchanges the values at the specified indices of the collection.

## Declaration

```swift
mutating func swapAt(_ i: Self.Index, _ j: Self.Index)
```

## Parameters

- `i`: The index of the first value to swap.
- `j`: The index of the second value to swap.

## Discussion

Discussion Both parameters must be valid indices of the collection that are not equal to endIndex. Calling swapAt(_:_:) with the same index as both i and j has no effect. note: O(1)

## See Also

### Reordering an Array’s Elements

- [sort()](swift/array/sort().md)
- [sort(by:)](swift/array/sort(by:).md)
- [sorted()](swift/array/sorted().md)
- [sorted(by:)](swift/array/sorted(by:).md)
- [reverse()](swift/array/reverse().md)
- [reversed()](swift/array/reversed().md)
- [shuffle()](swift/array/shuffle().md)
- [shuffle(using:)](swift/array/shuffle(using:).md)
- [shuffled()](swift/array/shuffled().md)
- [shuffled(using:)](swift/array/shuffled(using:).md)
- [partition(by:)](swift/array/partition(by:)-90po8.md)
