Make Array conditionally conform to Comparable?

The latter (which is what I know as the “lexicographical order”). This is also used for tuple comparison. An implementation could look like this:

extension Array: Comparable where Element: Comparable {
    public static func < (lhs: [Element], rhs: [Element]) -> Bool {
        for (leftElement, rightElement) in zip(lhs, rhs) {
            if leftElement < rightElement {
                return true
            } else if leftElement > rightElement {
                return false
            }
        }
        return lhs.count < rhs.count
    }
}
1 Like