Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save florianpircher/a69fa36cf335ddc4c6cfb07ad204bc8c to your computer and use it in GitHub Desktop.
Save florianpircher/a69fa36cf335ddc4c6cfb07ad204bc8c to your computer and use it in GitHub Desktop.
extension Sequence {
func sorted<SortKey>(
by areInIncreasingOrder: (SortKey, SortKey) throws -> Bool,
sortKey makeSortKey: (Element) throws -> SortKey
) rethrows -> [Element] {
try self
.map { (key: try makeSortKey($0), element: $0) }
.sorted { try areInIncreasingOrder($0.key, $1.key) }
.map { $0.element }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment