Last active
December 18, 2017 11:06
-
-
Save Marcocanc/bfb65f9786cf6ab6f26e45993ba2b515 to your computer and use it in GitHub Desktop.
Array sorting by keypath
This file contains hidden or 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 sorted<T: Comparable>(by keyPath: KeyPath<Element, T>, ordered areInIncreasingOrder: (T, T) -> Bool = (<) ) -> Array<Element> { | |
return self.sorted(by: { | |
areInIncreasingOrder($0[keyPath: keyPath], $1[keyPath: keyPath]) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment