Skip to content

Instantly share code, notes, and snippets.

@prufrock
Last active January 23, 2024 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prufrock/ce55390dcc9ca7b6d2e431c706802046 to your computer and use it in GitHub Desktop.
Save prufrock/ce55390dcc9ca7b6d2e431c706802046 to your computer and use it in GitHub Desktop.
Combine two arrays in swift into an array of pairs sorted by the first.
extension Array where Element: Comparable {
/// Combine both arrays into pairs and sort the result based on the first element.
/// - Parameter other: The array of elements to combine with.
/// - Returns: An array of pairs.
func aligned<T>(with other: Array<T>) -> [(Element, T)] {
let aligned = zip(self, other)
return aligned.sorted { $0.0 < $1.0 }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment