Ord

Custom ordering and comparison for any type

What is Ord?

Ord defines how to compare and order values of a type. Create custom sorting logic, find min/max values, and compose orderings together. Type-safe comparisons that work with any data structure.

// Instead of manual comparisons:
arr.sort((a, b) => a.age - b.age)

// Use Ord:
const byAge = pipe(N.Ord, Ord.contramap((u: User) => u.age))
const sorted = A.sort(byAge)(arr)
compare
contramap
reverse
min/max
sorting
🔢

Custom Ordering

Define how to compare any type

Type-Safe

Comparisons checked at compile time

🔗

Composable

Build complex orderings from simple ones

⚙️

Practical

Sorting, min, max, clamp operations

Ord in Action

❌ Manual Comparisons

users.sort((a, b) => {
  if (a.age < b.age) return -1
  if (a.age > b.age) return 1
  return 0
})

// Verbose, repetitive

✅ Ord Approach

const byAge = pipe(
  N.Ord,
  Ord.contramap((user: User) => user.age)
)

const sorted = A.sort(byAge)(users)
// Composable, reusable

Practice Exercises

1

Basic

Learn about basic in fp-ts ord

beginnerStart
2

Contramap

Learn about contramap in fp-ts ord

beginnerStart
3

Reverse

Learn about reverse in fp-ts ord

beginnerStart
4

Min Max

Learn about min max in fp-ts ord

intermediateStart
5

Sort

Learn about sort in fp-ts ord

intermediateStart
6

Practical

Learn about practical in fp-ts ord

intermediateStart

Why Learn Ord?

🔢

Master Ord

Learn the fundamental concepts and patterns that make Ord powerful

💪

6 Exercises

Practice with hands-on exercises from intermediate level

🚀

Production Ready

Apply Ord patterns to build robust, type-safe applications