gleam/order
Types
Functions
pub fn break_tie(in order: Order, with other: Order) -> Order
Return a fallback Order in case the first argument is Eq.
Examples
import gleam/int
break_tie(in: int.compare(1, 1), with: Lt)
// -> Lt
import gleam/int
break_tie(in: int.compare(1, 0), with: Eq)
// -> Gt
pub fn compare(a: Order, with b: Order) -> Order
Compares two Order values to one another, producing a new Order.
Examples
compare(Eq, with: Lt)
// -> Gt
pub fn lazy_break_tie(
in order: Order,
with comparison: fn() -> Order,
) -> Order
Invokes a fallback function returning an Order in case the first argument
is Eq.
This can be useful when the fallback comparison might be expensive and it needs to be delayed until strictly necessary.
Examples
import gleam/int
lazy_break_tie(in: int.compare(1, 1), with: fn() { Lt })
// -> Lt
import gleam/int
lazy_break_tie(in: int.compare(1, 0), with: fn() { Eq })
// -> Gt
pub fn negate(order: Order) -> Order
Inverts an order, so less-than becomes greater-than and greater-than becomes less-than.
Examples
negate(Lt)
// -> Gt
negate(Eq)
// -> Eq
negate(Gt)
// -> Lt
pub fn reverse(orderer: fn(a, a) -> Order) -> fn(a, a) -> Order
Inverts an ordering function, so less-than becomes greater-than and greater-than becomes less-than.
Examples
import gleam/int
import gleam/list
list.sort([1, 5, 4], by: reverse(int.compare))
// -> [5, 4, 1]