Last active
December 12, 2018 23:35
-
-
Save kharmabum/acbb66c255d837e9ce028ab082c20dd0 to your computer and use it in GitHub Desktop.
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
/// Sort any sequence by keypath conforming to Comparable | |
extension Sequence { | |
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>) -> [Element] { | |
return sorted { a, b in | |
return a[keyPath: keyPath] < b[keyPath: keyPath] | |
} | |
} | |
} | |
/// Example usage | |
playlist.songs.sorted(by: \.ratings.worldWide) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment