Skip to content

Instantly share code, notes, and snippets.

@msewell
Created March 31, 2021 07:58
Show Gist options
  • Save msewell/6b7b8818cbe9d71ec748322bac885fa5 to your computer and use it in GitHub Desktop.
Save msewell/6b7b8818cbe9d71ec748322bac885fa5 to your computer and use it in GitHub Desktop.
sort by key path
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