Skip to content

Instantly share code, notes, and snippets.

@loromits
Created October 27, 2017 07:17
Show Gist options
  • Save loromits/b77e8a7bb7abd18ca67517c74473fd6e to your computer and use it in GitHub Desktop.
Save loromits/b77e8a7bb7abd18ca67517c74473fd6e to your computer and use it in GitHub Desktop.
Add stableSorted to Swift collections
extension RandomAccessCollection {
func stableSorted(by areInIncreasingOrder: (Self.Element, Self.Element) -> Bool) -> [Self.Element] {
return enumerated()
.sorted { first, second in
return areInIncreasingOrder(first.element, second.element) || first.offset < second.offset
}
.map { $0.element }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment