Compare arrays of custom objects in Swift?

123 Views Asked by At

I have objects which implement Сomparable protocol:

class SomeClass: Comparable {
    ...//comparable implementation
}

So now I can for example sort [SomeClass].

But if I want to compare arrays of such objects?

Binary operator '<' cannot be applied to two '[SomeClass]' operands

How to solve this issue?

1

There are 1 best solutions below

1
rob mayoff On BEST ANSWER

I think you are looking for the lexicographicallyPrecedes(_:) method of Sequence.

There is also a lexicographicallyPrecedes(_:by:) method that takes a comparison function, in case your elements aren't Comparable (or you don't want to use the conformance).