Created
March 31, 2021 07:58
-
-
Save msewell/6b7b8818cbe9d71ec748322bac885fa5 to your computer and use it in GitHub Desktop.
sort by key path
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 Sequence { | |
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>, _ areInIncreasingOrder: ((T, T) -> Bool) = (<)) -> [Element] { | |
return sorted { a, b in | |
return areInIncreasingOrder(a[keyPath: keyPath], b[keyPath: keyPath]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment