Home > other >  Compare arrays of custom objects in Swift?
Compare arrays of custom objects in Swift?

Time:01-31

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?

CodePudding user response:

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).

  • Related