Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.