Skip to content

Instantly share code, notes, and snippets.

@mattt
Created November 27, 2018 22:34
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattt/9654e0571e1e252a22a9e6bf73e41277 to your computer and use it in GitHub Desktop.
Save mattt/9654e0571e1e252a22a9e6bf73e41277 to your computer and use it in GitHub Desktop.
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