Skip to content

Instantly share code, notes, and snippets.

@RobertKoval
Last active November 8, 2023 20:52
Show Gist options
  • Save RobertKoval/382801b882675f011fba1e69b120abc8 to your computer and use it in GitHub Desktop.
Save RobertKoval/382801b882675f011fba1e69b120abc8 to your computer and use it in GitHub Desktop.
Swift Comparable Array extension
[3,2,1] > [2,1,0] // true
[3,2,1] > [2,10,1000] // true
[3,4,2] < [3,4,3] // true
[3,4,2] > [2,4] // true
[3,4,2] == [3,4,2] // true
extension Array: Comparable where Element: Comparable {
public static func < (lhs: Array<Element>, rhs: Array<Element>) -> Bool {
for (a, b) in zip(lhs, rhs) where a != b {
return a < b
}
return lhs.count < rhs.count
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment