Ordering

public enum Ordering

The 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

The two values compared equal.

case Greater

The receiver compared greater than the argument.

case Less

The receiver compared less than the argument.

Methods

public func reverse() -> Ordering

Swaps Less and Greater; leaves Equal alone. Useful for sorting in reverse without writing a second comparator.

Examples

Ordering.Less.reverse() // .Greater Ordering.Equal.reverse() // .Equal
public func then(Ordering) -> Ordering

Tie-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

Lazy variant of thencompare runs only when self is Equal. Prefer this when computing the secondary comparison is expensive.

ImplementsEquatable

Associated Types

type Output = Bool

Methods

public func equal(to: Self) -> Bool

Bridges Equal.equal(to:) to Equatable.isEqual(to:).

public func isEqual(to: Ordering) -> Bool

Equality on the orderings themselves: same variant ⇒ equal.

public func notEqual(to: Self) -> Bool

Default !=: delegates to == so there's a single source of truth.

ImplementsFormattable

Methods

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

Returns 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