Skip to content

Instantly share code, notes, and snippets.

@kharmabum
Last active December 12, 2018 23:35
Show Gist options
  • Save kharmabum/acbb66c255d837e9ce028ab082c20dd0 to your computer and use it in GitHub Desktop.
Save kharmabum/acbb66c255d837e9ce028ab082c20dd0 to your computer and use it in GitHub Desktop.
/// 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