<A>(O: Order<A>): Order<A>Creates a new Order that reverses the comparison order of the input Order.
When to use
Use when you need the reverse of an existing order.
Details
Returns a new order that swaps the arguments before comparison. If the
original order returns -1, the flipped order returns 1, and vice versa.
Equal comparisons remain 0.
Example (Reversing an Order)
import { Order } from "effect"
const flip = Order.flip(Order.Number)
console.log(flip(1, 2)) // 1
console.log(flip(2, 1)) // -1
console.log(flip(1, 1)) // 0export function function flip<A>(O: Order<A>): Order<A>Creates a new Order that reverses the comparison order of the input Order.
When to use
Use when you need the reverse of an existing order.
Details
Returns a new order that swaps the arguments before comparison. If the
original order returns -1, the flipped order returns 1, and vice versa.
Equal comparisons remain 0.
Example (Reversing an Order)
import { Order } from "effect"
const flip = Order.flip(Order.Number)
console.log(flip(1, 2)) // 1
console.log(flip(2, 1)) // -1
console.log(flip(1, 1)) // 0
flip<function (type parameter) A in flip<A>(O: Order<A>): Order<A>A>(type O: Order<A>O: interface Order<in A>Represents a total ordering for values of type A.
When to use
Use when you need to define how values of a type are compared.
Details
An order returns -1 when the first value is less than the second, 0 when
the values are equal according to this ordering, and 1 when the first value
is greater than the second. It must satisfy total ordering laws: totality,
antisymmetry, and transitivity.
Example (Defining a custom Order)
import { Order } from "effect"
const byAge: Order.Order<{ name: string; age: number }> = (self, that) => {
if (self.age < that.age) return -1
if (self.age > that.age) return 1
return 0
}
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 25 }
console.log(byAge(person1, person2)) // 1
Order<function (type parameter) A in flip<A>(O: Order<A>): Order<A>A>): interface Order<in A>Represents a total ordering for values of type A.
When to use
Use when you need to define how values of a type are compared.
Details
An order returns -1 when the first value is less than the second, 0 when
the values are equal according to this ordering, and 1 when the first value
is greater than the second. It must satisfy total ordering laws: totality,
antisymmetry, and transitivity.
Example (Defining a custom Order)
import { Order } from "effect"
const byAge: Order.Order<{ name: string; age: number }> = (self, that) => {
if (self.age < that.age) return -1
if (self.age > that.age) return 1
return 0
}
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 25 }
console.log(byAge(person1, person2)) // 1
Order<function (type parameter) A in flip<A>(O: Order<A>): Order<A>A> {
return function make<A>(
compare: (self: A, that: A) => -1 | 0 | 1
): Order<A>
Creates a new Order instance from a comparison function.
When to use
Use when you need a sorting rule not covered by the built-in orders or input
mapping helpers, and you can provide a total comparison.
Details
Uses reference equality (===) as a shortcut: if self === that, it returns
0 without calling the comparison function. The comparison function should
return -1, 0, or 1, and the returned order satisfies total ordering
laws when the comparison function does.
Example (Creating an Order)
import { Order } from "effect"
const byAge = Order.make<{ name: string; age: number }>((self, that) => {
if (self.age < that.age) return -1
if (self.age > that.age) return 1
return 0
})
console.log(byAge({ name: "Alice", age: 30 }, { name: "Bob", age: 25 })) // 1
console.log(byAge({ name: "Alice", age: 25 }, { name: "Bob", age: 30 })) // -1
make((self: Aself, that: Athat) => type O: Order<A>O(that: Athat, self: Aself))
}