Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- extension Array where Element: Equatable
- {
- mutating func move(_ element: Element, to newIndex: Index) {
- if let oldIndex: Int = self.firstIndex(of: element) { self.move(from: oldIndex, to: newIndex) }
- }
- }
- extension Array where Element == Dictionary<String, Any> {
- mutating func move(_ element:Element, to newIndex: Index) {
- if let oldIndex = self.firstIndex(where: { ($0.keys.first ?? "") == (element.keys.first ?? "") }) {
- self.move(from: oldIndex, to: newIndex)
- }
- }
- }
- extension Array
- {
- mutating func move(from oldIndex: Index, to newIndex: Index) {
- // Don't work for free and use swap when indices are next to each other - this
- // won't rebuild array and will be super efficient.
- if oldIndex == newIndex { return }
- if abs(newIndex - oldIndex) == 1 { return self.swapAt(oldIndex, newIndex) }
- self.insert(self.remove(at: oldIndex), at: newIndex)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement