Skip to content

Instantly share code, notes, and snippets.

@lilyball
Last active March 12, 2019 21:01
Show Gist options
  • Save lilyball/76a0146fa7184ee8b85e57f4db4a078d to your computer and use it in GitHub Desktop.
Save lilyball/76a0146fa7184ee8b85e57f4db4a078d to your computer and use it in GitHub Desktop.
extension Collection {
func sorted<T: Comparable>(over: (Element) -> T) -> [Element] {
var projections: [(T, Index)] = indices.map({ (over(self[$0]), $0) })
projections.sort(by: { $0.0 < $1.0 })
return projections.map({ self[$0.1] })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment