Ramda.js R.comparator() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Makes a comparator function out of a function that reports whether the first element is less than the second (official Ramda docs). You write a boolean less-than test; Ramda returns -1, 0, or 1 for R.sort.

Stable since v0.1.0: R.comparator is a core Ramda helper — not deprecated, experimental, or non-standard. Prefer R.ascend when a single-field getter is enough.

Syntax

Call R.comparator. Start with this line.

Ramda
R.comparator(pred)
CallWhat it does
R.comparator(pred)Primary signature.
R.comparator(...)Call the function helper.

Prefer import { comparator } from "ramda" when you want a named import.

Library declarations
Ramda
R.comparator(pred)

Parameters

ParameterDescription
predRequired. Two-arg function; true means first < second

Return Value

Return ValueDescription
*Comparator (a, b) → Number (−1, 0, or 1)

Ramda Examples

Example 1 — Official byAge Sort

Youngest first — the sample from the Ramda docs.

Ramda
const byAge = R.comparator((a, b) => a.age < b.age);
const people = [
  { name: 'Emma', age: 70 },
  { name: 'Peter', age: 78 },
  { name: 'Mikhail', age: 62 }
];

console.log(R.sort(byAge, people));
Try It Yourself

How It Works

  1. Write the less-than test. (a, b) => a.age < b.age answers “is a younger than b?”
  2. Wrap with R.comparator. That predicate becomes a -1 / 0 / 1 function named byAge.
  3. Sort immutably. R.sort(byAge, people) returns Mikhail (62), Emma (70), Peter (78).
  4. Source stays intact. people is unchanged — only the returned array is ordered.

Example 2 — Comparator Signs

Call the comparator on pairs without sorting the whole list.

Ramda
const byAge = R.comparator((a, b) => a.age < b.age);
const younger = { name: 'Mikhail', age: 62 };
const older = { name: 'Emma', age: 70 };

console.log(byAge(younger, older)); // -1
console.log(byAge(older, younger)); // 1
console.log(byAge(older, older));   // 0
Try It Yourself

How It Works

  1. Younger first. 62 < 70 is true → pred(a, b) → -1.
  2. Swap the pair. Now pred(b, a) is true → 1 (older should come after).
  3. Equal ages. Both less-than tests are false → 0 (tie).
  4. Contract complete. Those three numbers are all R.sort needs.

Example 3 — Sort Numbers Smallest First

When the list holds comparable values, compare the items themselves.

Ramda
const byValue = R.comparator((a, b) => a < b);

console.log(R.sort(byValue, [30, 4, 21]));
Try It Yourself

How It Works

  1. Predicate on values. (a, b) => a < b is a numeric less-than test.
  2. Avoid default string sort. Native [30, 4, 21].sort() would put 21 before 4.
  3. Numeric order. R.sort(byValue, …) yields [4, 21, 30].
  4. Reuse the helper. Name it once and pass it to any list of numbers.

Example 4 — R.comparator vs R.ascend

Same people, same result — predicate vs getter.

Ramda
const people = [
  { name: 'Emma', age: 70 },
  { name: 'Peter', age: 78 },
  { name: 'Mikhail', age: 62 }
];

const byPred = R.comparator((a, b) => a.age < b.age);
const byGetter = R.ascend(R.prop('age'));

console.log(R.sort(byPred, people));
console.log(R.sort(byGetter, people));
Try It Yourself

How It Works

  1. Predicate style. R.comparator wants (a, b) => a.age < b.age.
  2. Getter style. R.ascend(R.prop('age')) extracts age, then compares.
  3. Same order. Both sorts list Mikhail, Emma, Peter.
  4. Choose by rule shape. Single field → R.ascend; custom less-than → R.comparator.

Example 5 — Flip the Predicate for Descending

Change < to > so oldest comes first.

Ramda
const people = [
  { name: 'Emma', age: 70 },
  { name: 'Peter', age: 78 },
  { name: 'Mikhail', age: 62 }
];

const youngestFirst = R.comparator((a, b) => a.age < b.age);
const oldestFirst = R.comparator((a, b) => a.age > b.age);

console.log(R.sort(youngestFirst, people));
console.log(R.sort(oldestFirst, people));
Try It Yourself

How It Works

  1. Ascending. < means smaller age first → Mikhail … Peter.
  2. Flip the question. > means larger age first → Peter … Mikhail.
  3. Same helper. No extra API — only the boolean changes.
  4. Getter twin. For field-only descending, prefer R.descend.

Browser & Runtime Support

R.comparator() is ordinary JavaScript once Ramda is loaded. It needs no special browser API — only a JS runtime that can run Ramda (v0.1.0+).

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.comparator()

Works in every modern browser and runtime where you can load Ramda (CDN or npm). Internet Explorer needs a build that targets that environment; there is no native IE API to polyfill.

100% With Ramda loaded
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.comparator() Universal

Bottom line: Safe in any project that includes Ramda 0.1.0 or later. Load ramda.min.js from a CDN in the browser, or npm install ramda for Node, Bun, and Deno.

Frequently Asked Questions

R.comparator(pred) takes a two-argument predicate that returns true when the first value is less than the second. It returns a sort comparator that yields -1, 1, or 0. Pair that comparator with R.sort.
const byAge = R.comparator((a, b) => a.age < b.age); then R.sort(byAge, people) on Emma (70), Peter (78), and Mikhail (62) yields Mikhail, Emma, Peter. Added in v0.1.0.
The comparator returns -1 if pred(a, b) is true (a should come first), 1 if pred(b, a) is true (b should come first), and 0 when neither is less than the other (a tie).
R.ascend(fn) takes a getter such as R.prop('age') and compares fn(a) < fn(b). R.comparator(pred) takes a boolean less-than test such as (a, b) => a.age < b.age. Both produce the same -1 / 0 / 1 function for R.sort.
No. R.sort(byAge, people) returns a new array. Native array.sort() mutates in place. Prefer R.sort with the comparator when you want an immutable sort.
No. R.comparator is a stable Ramda function helper from v0.1.0. It is not deprecated, experimental, or non-standard.

Next: R.compose()

Chain functions right-to-left into one reusable pipeline.

R.compose() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful