The Order module provides a way to compare values and determine their order.
It defines an interface Order<A> which represents a single function for comparing two values of type A.
The function returns -1, 0, or 1, indicating whether the first value is less than, equal to, or greater than the second value.
Here’s the basic structure of an Order:
Using the Built-in Orders
The Order module comes with several built-in comparators for common data types:
Order
Description
string
Used for comparing strings.
number
Used for comparing numbers.
bigint
Used for comparing big integers.
Date
Used for comparing Date objects.
Example (Using Built-in Comparators)
Sorting Arrays
You can sort arrays using these comparators. The Array module offers a sort function that sorts arrays without altering the original one.
Example (Sorting Arrays with Order)
You can also use an Order as a comparator with JavaScript’s native Array.sort method, but keep in mind that this will modify the original array.
Example (Using Order with Native Array.prototype.sort)
Deriving Orders
For more complex data structures, you may need custom sorting rules. The Order module lets you derive new Order instances from existing ones with the Order.mapInput function.
Example (Creating a Custom Order for Objects)
Imagine you have a list of Person objects, and you want to sort them by their names in ascending order.
To achieve this, you can create a custom Order.
The Order.mapInput function takes two arguments:
The existing Order you want to use as a base (Order.string in this case, for comparing strings).
A function that extracts the value you want to use for sorting from your data structure ((person: Person) => person.name in this case).
Once you have defined your custom Order, you can apply it to sort an array of Person objects:
Example (Sorting Objects Using a Custom Order)
Combining Orders
The Order module lets you combine multiple Order instances to create complex sorting rules. This is useful when sorting by multiple properties.
Example (Sorting by Multiple Criteria)
Imagine you have a list of people, each represented by an object with a name and an age. You want to sort this list first by name and then, for individuals with the same name, by age.
Additional Useful Functions
The Order module provides additional functions for common comparison operations, making it easier to work with ordered values.
Reversing Order
Order.reverse inverts the order of comparison. If you have an Order for ascending values, reversing it makes it descending.
Example (Reversing an Order)
Comparing Values
These functions allow you to perform simple comparisons between values:
API
Description
lessThan
Checks if one value is strictly less than another.
greaterThan
Checks if one value is strictly greater than another.
lessThanOrEqualTo
Checks if one value is less than or equal to another.
greaterThanOrEqualTo
Checks if one value is greater than or equal to another.
Example (Using Comparison Functions)
Finding Minimum and Maximum
The Order.min and Order.max functions return the minimum or maximum value between two values, considering the order.
Example (Finding Minimum and Maximum Numbers)
Clamping Values
Order.clamp restricts a value within a given range. If the value is outside the range, it is adjusted to the nearest bound.
Example (Clamping Numbers to a Range)
Checking Value Range
Order.between checks if a value falls within a specified inclusive range.