Ramda.js R.sort() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.sort(comparator, list) returns a sorted copy of a list. The comparator looks at two items at a time and says which one goes first (official Ramda docs). The original list is never changed, and because the list comes last, R.sort(cmp) is a reusable function for pipelines.

Stable since v0.1.0: R.sort is not deprecated, experimental, or non-standard. Unlike native Array.prototype.sort(), it never changes the original array — the same idea as the newer native toSorted() (ES2023).

Syntax

Call R.sort. Start with this line.

Ramda
R.sort(cmp, list) or R.sort(cmp)(list)
CallWhat it does
R.sort(cmp, list) or R.sort(cmp)(list)Primary signature.
R.sort(...)Call the list helper.

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

Library declarations
Ramda
R.sort(cmp, list) or R.sort(cmp)(list)

Parameters

ParameterDescription
comparatorRequired. Function (a, b) => Number. Decides the order of any two items
listOptional. Array. The items to sort; omit it to get a reusable function

Return Value

Return ValueDescription
*A new sorted array

Ramda Examples

Example 1 — Sort Numbers (Official Sample)

Sort numbers from smallest to largest with the diff comparator from the Ramda docs.

Ramda
const diff = (a, b) => a - b;
const nums = [4, 2, 7, 5];

console.log(R.sort(diff, nums)); // [2, 4, 5, 7]
console.log(nums);               // [4, 2, 7, 5]
Try It Yourself

How It Works

  1. Comparator. diff(a, b) returns a - b: negative when a is smaller.
  2. Negative means first. A negative result puts a before b, so small numbers move to the front.
  3. Sorted copy. R.sort returns [2, 4, 5, 7].
  4. No mutation. nums is still [4, 2, 7, 5].

Example 2 — Descending, and Why It Beats Native sort

Flip the subtraction for largest-first, then compare with native sort, which changes the array itself.

Ramda
const scores = [40, 100, 1, 5, 25];

const ranked = R.sort((a, b) => b - a, scores);
console.log(ranked); // [100, 40, 25, 5, 1]
console.log(scores); // [40, 100, 1, 5, 25]  untouched

const copy = [40, 100, 1, 5, 25];
copy.sort((a, b) => b - a);
console.log(copy);   // [100, 40, 25, 5, 1]  changed in place
Try It Yourself

How It Works

  1. Flip the sign. b - a is negative when a is larger, so big numbers come first.
  2. Ramda copies. ranked is a new array; scores keeps its original order.
  3. Native mutates. copy.sort() reorders copy itself — any other code holding that array sees the change.
  4. Same comparator. Both accept the same (a, b) => number function; only the mutation differs.

Example 3 — Sort Strings Alphabetically

Use localeCompare for text. Plain code-unit order puts capital letters first.

Ramda
const fruits = ['banana', 'apple', 'Cherry'];

const byName = (a, b) => a.localeCompare(b);

console.log(R.sort(byName, fruits)); // ['apple', 'banana', 'Cherry']
console.log([...fruits].sort());     // ['Cherry', 'apple', 'banana']
Try It Yourself

How It Works

  1. Text comparator. localeCompare returns a negative, positive, or zero number — exactly what R.sort needs.
  2. Human order. It compares letters the way a dictionary does, so 'Cherry' lands after 'banana'.
  3. Default order differs. Native sort() with no comparator uses character codes, where every capital letter comes before every lowercase one.
  4. Don’t subtract strings. 'b' - 'a' is NaN, which leaves the order unpredictable.

Example 4 — Sort Objects by a Field

Let R.ascend and R.descend build the comparator from a field.

Ramda
const people = [
  { name: 'Ana', age: 31 },
  { name: 'Ben', age: 25 },
  { name: 'Cy',  age: 40 }
];

const youngest = R.sort(R.ascend(R.prop('age')), people);
const oldest   = R.sort(R.descend(R.prop('age')), people);

console.log(R.pluck('name', youngest)); // ['Ben', 'Ana', 'Cy']
console.log(R.pluck('name', oldest));   // ['Cy', 'Ana', 'Ben']
Try It Yourself

How It Works

  1. Pick the field. R.prop('age') reads the age of one person.
  2. Build the comparator. R.ascend(fn) compares fn(a) with fn(b) — smallest first. R.descend reverses it.
  3. Sort. Ages 25, 31, 40 give Ben, Ana, Cy; descending gives Cy, Ana, Ben.
  4. Show names. R.pluck('name') pulls out just the names for a short printout.

Example 5 — Break Ties with a Second Rule

Sort by team first; when the team is the same, sort by age.

Ramda
const staff = [
  { name: 'Dee', team: 'web', age: 35 },
  { name: 'Eve', team: 'app', age: 29 },
  { name: 'Fay', team: 'web', age: 24 },
  { name: 'Gus', team: 'app', age: 41 }
];

const byTeamThenAge = (a, b) =>
  a.team.localeCompare(b.team) || a.age - b.age;

console.log(R.pluck('name', R.sort(byTeamThenAge, staff)));
// ['Eve', 'Gus', 'Fay', 'Dee']
Try It Yourself

How It Works

  1. First rule. a.team.localeCompare(b.team) puts 'app' before 'web'.
  2. Tie means 0. When both are on the same team, that result is 0, and 0 || … moves on to the next rule.
  3. Second rule. a.age - b.age orders teammates youngest first: Eve (29) before Gus (41), Fay (24) before Dee (35).
  4. Ramda shortcut. R.sortWith([R.ascend(R.prop('team')), R.ascend(R.prop('age'))]) gives the same result.

Browser & Runtime Support

R.sort() is plain JavaScript inside Ramda 0.1.0+. It copies the list and calls the built-in Array.prototype.sort, so support depends only on loading Ramda. Equal items keep their order wherever the engine's sort is stable (guaranteed since ES2019).

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.sort()

The UMD build (ramda.min.js) uses ES5 syntax, so it runs in every modern browser. Via npm it works in Node.js, Deno, and Bun. Stable ordering of ties is guaranteed in Chrome 70+, Edge 79+, current Firefox and Safari, and Node.js 12+.

100% With Ramda loaded
Google Chrome Supported · stable sort since 70
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium 79+
Full support
Internet Explorer End of life · ES5 build may load, ties not guaranteed stable
Legacy only
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Node.js Supported · stable sort since 12
Supported
Deno Supported · npm:ramda
Supported
Bun Supported · bun add ramda
Supported
R.sort() Universal

Bottom line: Safe in any project that loads Ramda. Use ramda.min.js from a CDN in the browser, or npm install ramda for Node, Deno, and Bun. The Try-it labs load [email protected] and use modern syntax (const, arrow functions).

Frequently Asked Questions

A new array sorted by your comparator. R.sort((a, b) => a - b, [4, 2, 7, 5]) returns [2, 4, 5, 7]. The original list is not changed.
No. Ramda copies the list first and sorts the copy. Native Array.prototype.sort() sorts in place and changes the original array.
A negative number if a should come first, a positive number if b should come first, and 0 if they are equal. For numbers, (a, b) => a - b sorts ascending and (a, b) => b - a sorts descending. Do not return true or false.
Build the comparator with R.ascend or R.descend: R.sort(R.ascend(R.prop('age')), people) sorts youngest first, and R.descend(R.prop('age')) sorts oldest first.
R.sort takes a two-argument comparator. R.sortBy takes a one-argument key function and sorts ascending by that key. R.sortWith takes a list of comparators and uses the next one to break ties.
R.sort uses the engine's native sort, which is guaranteed stable since ES2019, so equal items keep their original order in modern browsers and Node.js 12+. R.sort has been stable in Ramda since v0.1.0 and is not deprecated, experimental, or non-standard.

Next: R.splitAt()

Split a list or string into two parts at an index.

R.splitAt() 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.

6 people found this page helpful