Ordering
public enum OrderingThe three-valued result of a Comparable.compare() call.
Ordering is the lingua franca for comparison: types implementing
Comparable define a single compare returning this enum, and the
stdlib derives <, <=, >, >= on top. The then / thenWith
helpers make it easy to chain comparisons over multiple fields without
nested ifs.
Examples
let cmp = a.compare(b);
match cmp {
.Less => "ascending",
.Equal => "tied",
.Greater => "descending"
}
// Chain field comparisons: by lastName, then firstName.
a.lastName.compare(b.lastName)
.then(a.firstName.compare(b.firstName))
Representation
A plain three-state enum with no payload — lowers to a small integer tag.
Cases
case Equal
case EqualThe two values compared equal.
case Greater
case GreaterThe receiver compared greater than the argument.
case Less
case LessThe receiver compared less than the argument.
Methods
public func reverse() -> Ordering
public func reverse() -> OrderingSwaps Less and Greater; leaves Equal alone. Useful for sorting
in reverse without writing a second comparator.
Examples
Ordering.Less.reverse() // .Greater
Ordering.Equal.reverse() // .Equalpublic func then(Ordering) -> Ordering
public func then(Ordering) -> OrderingTie-breaker chain: returns self if it is non-Equal, otherwise
other. The eager form — both arguments are evaluated.
Examples
Ordering.Equal.then(.Less) // .Less
Ordering.Greater.then(.Less) // .Greater (self wins)public func thenWith(() -> Ordering) -> Ordering
public func thenWith(() -> Ordering) -> OrderingLazy variant of then — compare runs only when self is Equal.
Prefer this when computing the secondary comparison is expensive.
ImplementsEquatable
Associated Types
type Output = Bool
type Output = BoolMethods
public func equal(to: Self) -> Bool
public func equal(to: Self) -> BoolBridges Equal.equal(to:) to Equatable.isEqual(to:).
public func isEqual(to: Ordering) -> Bool
public func isEqual(to: Ordering) -> BoolEquality on the orderings themselves: same variant ⇒ equal.
public func notEqual(to: Self) -> Bool
public func notEqual(to: Self) -> BoolDefault !=: delegates to == so there's a single source of truth.
ImplementsFormattable
Methods
public func format(into: mutating StringBuilder, FormatOptions)
public func format(into: mutating StringBuilder, FormatOptions)Renders as "Less", "Equal", or "Greater". With debug set,
prefixes with the type name ("Ordering.Less").
public func formatted(FormatOptions) -> String
public func formatted(FormatOptions) -> StringReturns this value rendered as a String.
Convenience wrapper: creates a StringBuilder, calls
format(into:), and returns the built string. Uses a distinct
name to avoid overload-resolution ambiguity with format(into:).
Defined in lang/std/core/ordering.ks