(o: Ordering): OrderingReverses the ordering of the input Ordering. This is useful for creating descending sort orders from ascending ones.
When to use
Use to flip an ordering result when reversing sort direction or comparison priority.
Example (Reversing comparison order)
import { Ordering } from "effect"
// Basic reversal
console.log(Ordering.reverse(1)) // -1 (greater becomes less)
console.log(Ordering.reverse(-1)) // 1 (less becomes greater)
console.log(Ordering.reverse(0)) // 0 (equal stays equal)
// Creating descending sort from ascending comparison
const compareNumbers = (a: number, b: number): Ordering.Ordering =>
a < b ? -1 : a > b ? 1 : 0
const compareDescending = (a: number, b: number): Ordering.Ordering =>
Ordering.reverse(compareNumbers(a, b))
const numbers = [3, 1, 4, 1, 5]
numbers.sort(compareNumbers) // [1, 1, 3, 4, 5] (ascending)
numbers.sort(compareDescending) // [5, 4, 3, 1, 1] (descending)
// Useful for toggling sort direction
const createSorter = (ascending: boolean) => (a: number, b: number) => {
const ordering = compareNumbers(a, b)
return ascending ? ordering : Ordering.reverse(ordering)
}export const const reverse: (o: Ordering) => OrderingReverses the ordering of the input Ordering.
This is useful for creating descending sort orders from ascending ones.
When to use
Use to flip an ordering result when reversing sort direction or comparison
priority.
Example (Reversing comparison order)
import { Ordering } from "effect"
// Basic reversal
console.log(Ordering.reverse(1)) // -1 (greater becomes less)
console.log(Ordering.reverse(-1)) // 1 (less becomes greater)
console.log(Ordering.reverse(0)) // 0 (equal stays equal)
// Creating descending sort from ascending comparison
const compareNumbers = (a: number, b: number): Ordering.Ordering =>
a < b ? -1 : a > b ? 1 : 0
const compareDescending = (a: number, b: number): Ordering.Ordering =>
Ordering.reverse(compareNumbers(a, b))
const numbers = [3, 1, 4, 1, 5]
numbers.sort(compareNumbers) // [1, 1, 3, 4, 5] (ascending)
numbers.sort(compareDescending) // [5, 4, 3, 1, 1] (descending)
// Useful for toggling sort direction
const createSorter = (ascending: boolean) => (a: number, b: number) => {
const ordering = compareNumbers(a, b)
return ascending ? ordering : Ordering.reverse(ordering)
}
reverse = (o: Orderingo: 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): 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 => (o: Orderingo === -1 ? 1 : o: Orderingo === 1 ? -1 : 0)