<A>(collection: Iterable<Order<A>>): Order<A>Combines all Order instances in the provided collection into a single Order.
The resulting Order compares using each Order in sequence until a non-zero result is found.
When to use
Use when you need tie-breaking across a variable number of orders.
Details
Applies orders in iteration order and short-circuits on the first non-zero
result. It returns 0 only if all orders return 0.
Example (Combining multiple Orders)
import { Order } from "effect"
const byAge = Order.mapInput(
Order.Number,
(person: { name: string; age: number }) => person.age
)
const byName = Order.mapInput(
Order.String,
(person: { name: string; age: number }) => person.name
)
const combinedOrder = Order.combineAll([byAge, byName])
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 30 }
console.log(combinedOrder(person1, person2)) // -1 (Same age, Alice < Bob)export function function combineAll<A>(
collection: Iterable<Order<A>>
): Order<A>
Combines all Order instances in the provided collection into a single Order.
The resulting Order compares using each Order in sequence until a non-zero result is found.
When to use
Use when you need tie-breaking across a variable number of orders.
Details
Applies orders in iteration order and short-circuits on the first non-zero
result. It returns 0 only if all orders return 0.
Example (Combining multiple Orders)
import { Order } from "effect"
const byAge = Order.mapInput(
Order.Number,
(person: { name: string; age: number }) => person.age
)
const byName = Order.mapInput(
Order.String,
(person: { name: string; age: number }) => person.name
)
const combinedOrder = Order.combineAll([byAge, byName])
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 30 }
console.log(combinedOrder(person1, person2)) // -1 (Same age, Alice < Bob)
combineAll<function (type parameter) A in combineAll<A>(collection: Iterable<Order<A>>): Order<A>A>(collection: Iterable<Order<A>>collection: interface Iterable<T, TReturn = any, TNext = any>Iterable<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 combineAll<A>(collection: Iterable<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 combineAll<A>(collection: Iterable<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((a1: Aa1, a2: Aa2) => {
let let out: Orderingout: type Ordering = 0 | 1 | -1Represents the result of comparing two values.
When to use
Use to model a normalized comparison result that is exactly less than,
equal to, or greater than.
Details
-1 indicates the first value is less than the second
0 indicates the values are equal
1 indicates the first value is greater than the second
Example (Defining comparison results)
import type { Ordering } from "effect"
// Custom comparison function
const compareNumbers = (a: number, b: number): Ordering.Ordering => {
if (a < b) return -1
if (a > b) return 1
return 0
}
console.log(compareNumbers(5, 10)) // -1 (5 < 10)
console.log(compareNumbers(10, 5)) // 1 (10 > 5)
console.log(compareNumbers(5, 5)) // 0 (5 == 5)
// Using with string comparison
const compareStrings = (a: string, b: string): Ordering.Ordering => {
return a.localeCompare(b) as Ordering.Ordering
}
Ordering = 0
for (const const O: Order<A>O of collection: Iterable<Order<A>>collection) {
let out: Orderingout = const O: Order<A>O(a1: Aa1, a2: Aa2)
if (let out: Orderingout !== 0) {
return let out: Orderingout
}
}
return let out: Orderingout
})
}