Instance MethodSwift
lexicographicallyPrecedes(_:)
Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the less-than operator (<
) to compare elements.
Available when
Self.Element
conforms toComparable
func lexicographicallyPrecedes<OtherSequence>(_ other: OtherSequence) -> Bool where OtherSequence : Sequence, Self.Element == OtherSequence.Element
Parameters
- other
A sequence to compare to this sequence.
Returns
true
if this sequence precedes other
in a dictionary ordering; otherwise, false
.
Overview
This example uses the lexicographicallyPrecedes
method to test which array of integers comes first in a lexicographical ordering.
let a = [1, 2, 2, 2]
let b = [1, 2, 3, 4]
print(a.lexicographicallyPrecedes(b))
// Prints "true"
print(b.lexicographicallyPrecedes(b))
// Prints "false"