Created
November 27, 2018 22:34
-
-
Save mattt/9654e0571e1e252a22a9e6bf73e41277 to your computer and use it in GitHub Desktop.
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
import Foundation | |
extension Collection where Element: Comparable { | |
func sorted(ascending: Bool = true, using comparator: (Element) -> (Element) -> ComparisonResult) -> [Element] { | |
return self.sorted { lhs, rhs in | |
comparator(lhs)(rhs) == (ascending ? .orderedAscending : .orderedDescending) | |
} | |
} | |
} | |
let words = ["whisky", "tango", "foxtrot"] | |
words.sorted(using: String.localizedStandardCompare) | |
// => ["foxtrot", "tango", "whisky"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment