Created
October 27, 2017 07:17
-
-
Save loromits/b77e8a7bb7abd18ca67517c74473fd6e to your computer and use it in GitHub Desktop.
Add stableSorted to Swift collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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