Ordering
A value of the built-in type Ordering
is a value denoting that something is “less than”, “equal to”, or “greater than” something else:
type Ordering
| Less
| Equal
| Greater
compareInt : Int -> Int -> Ordering
compareFloat : Float -> Float -> Ordering
compareInstant : Instant -> Instant -> Ordering
compareDate : Date -> Date -> Ordering
compareTime : Time -> Time -> Ordering
Examples
val a = compareInt 3 6
// = Less
val b = compareFloat 4.5 4.5
// = Equal
val c = compareInstant #2017-12-24T12:00:00Z# #2017-10-11T10:00:00Z#
// = Greater
val d = compareDate (Date::from 2017 September 25) (Date::from 2017 September 19)
// = Greater
val e = compareTime (Time::from 12 0 0 0) (Time::from 13 0 0 0)
// = Less
Ordering::twoStep : (a -> a -> Ordering) -> (a -> a -> Ordering) -> a -> a -> Ordering
Ordering combinator ‘twoStep’ takes two functions comparing a given type. When comparing concrete arguments, if the first comparison function returns ‘Equal’, the second one is used to decide the comparison.
Examples
val a = Ordering::twoStep
(\(x: Tuple Int Float) -> \y -> compareInt (fst x) (fst y))
(\(x: Tuple Int Float) -> \y -> compareFloat (snd x) (snd y))
(5, 99.9) (7, 15.599)
// = Less
type R { a: Float, b: Int}
val b = Ordering::twoStep
(\(x: R) -> \(y: R) -> compareFloat (x.a) (y.a))
(\(x: R) -> \(y: R) -> compareInt (x.b) (y.b))
R {a = 3.14, b = 6} R {a = 3.14, b = 2}
// = Greater
Ordering::lexicographic : (a -> a -> Ordering) -> (a -> a -> Ordering) -> a -> a -> Ordering
Ordering combinator ‘lexicographic’ builds an ordering function for a binary tuple. It takes two arguments, comparison functions for the first and the second element of the tuple, and returns a lexicographic comparison on the tuples.
Examples
val a = Ordering::lexicographic compareInt compareFloat
(5, 99.9) (7, 15.599)
// = Less
type R { a: Float, b: Int}
val b = Ordering::lexicographic compareInt compareFloat
(5, 17.0) (5, 16.9)
// = Greater