Created
May 20, 2018 19:24
-
-
Save comm1x/75d7dea23379df6341ce96ae0724f89f to your computer and use it in GitHub Desktop.
Extension methods .sortedBy .sortedByDescending like in Kotlin
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 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