Skip to content

Instance Method

compare(_:_:)

iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ macOS 12.0+ tvOS 15.0+ watchOS 8.0+
Provides the relative ordering of two elements based on the sort order of the comparator.
func compare(_ lhs: Self.Compared, _ rhs: Self.Compared) -> ComparisonResult

Parameters

lhs

A value to compare.

rhs

A value to compare.

Return Value

The relative ordering of the two values.

Discussion

The result of comparisons should be flipped if the current order is reverse.

If compare(lhs, rhs) is .orderedAscending, then compare(rhs, lhs) must be .orderedDescending. If compare(lhs, rhs) is .orderedDescending, then compare(rhs, lhs) must be .orderedAscending.