Last active
January 23, 2024 22:01
-
-
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.
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 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