Skip to content

Instantly share code, notes, and snippets.

@comm1x
Created May 20, 2018 19:24
Show Gist options
  • Save comm1x/75d7dea23379df6341ce96ae0724f89f to your computer and use it in GitHub Desktop.
Save comm1x/75d7dea23379df6341ce96ae0724f89f to your computer and use it in GitHub Desktop.
Extension methods .sortedBy .sortedByDescending like in Kotlin
extension Array {
func sortedBy<T: Comparable>(_ cb: (Element) -> T) -> [Element] {
return self.sorted(by: { (l: Element, r: Element) -> Bool in
return cb(l) < cb(r)
})
}
func sortedByDescending<T: Comparable>(_ cb: (Element) -> T) -> [Element] {
return self.sorted(by: { (l: Element, r: Element) -> Bool in
return cb(l) > cb(r)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment