Ramda.js R.sortWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Relation

Definition

Ramda’s R.sortWith() answers a ranking question: order this list by several rules, in priority order. You pass an array of comparator functions, then the list. Each comparator returns a negative number, zero, or a positive number — the same contract as native Array.sort. Ramda walks that array until a comparator is non-zero; later ones only run on ties. Native Array.prototype.sort takes one comparator and rewrites the same array. sortWith is data-last, curried, and leaves the source list alone.

The official sample is the whole idea. Alice and Clara are both 40; Bob is 30. R.sortWith([R.descend(R.prop('age')), R.ascend(R.prop('name'))]) on [clara, bob, alice] is [alice, clara, bob]: forties first, then names A before C, then Bob. One projected key cannot do that — that is R.sortBy. One comparator is R.sort. Ready comparators from a key: R.ascend and R.descend. Previous helper is R.sortBy. Next is R.symmetricDifference, either-not-both of two lists as a unique set.

Syntax

Call R.sortWith. Start with this line.

Ramda
R.sortWith(functions, list)
CallWhat it does
R.sortWith(functions, list)Primary signature.
R.sortWith(...)Call the relation helper.

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

Library declarations
Ramda
R.sortWith(functions, list)

Parameters

ParameterDescription
functionsRequired. Array. A list of comparator functions. Each should return a negative number, 0, or a positive number, like Array.sort.
listRequired. Array. The collection to order. Ramda copies before sorting; this array is not mutated.

Return Value

Return ValueDescription
ArrayA new list sorted according to the comparator functions. Original items, new order.

Ramda Examples

📚 Getting Started

The official Ramda people sort: descend age, then ascend name.

Example 1 — Official ageNameSort

From the Ramda docs. Age descending first. On a 40/40 tie, name ascending: alice before clara.

Ramda
const alice = { name: 'alice', age: 40 };
const bob = { name: 'bob', age: 30 };
const clara = { name: 'clara', age: 40 };
const people = [clara, bob, alice];
const ageNameSort = R.sortWith([
  R.descend(R.prop('age')),
  R.ascend(R.prop('name'))
]);

ageNameSort(people);
//=> [alice, clara, bob]
Try It Yourself

How It Works

That is the docs sample from ramdajs.com. Age 40 beats age 30, so the forties come first. Alice and Clara tie on age; the second comparator sorts names ascending, so alice before clara. Bob is last.

Example 2 — vs R.sortBy(R.prop('age'))

One projected key cannot break the 40/40 name tie the way the official sortWith does.

Ramda
const alice = { name: 'alice', age: 40 };
const bob = { name: 'bob', age: 30 };
const clara = { name: 'clara', age: 40 };
const people = [clara, bob, alice];

R.sortBy(R.prop('age'), people);
//=> [bob, clara, alice]

R.sortWith([
  R.descend(R.prop('age')),
  R.ascend(R.prop('name'))
], people);
//=> [alice, clara, bob]
Try It Yourself

How It Works

sortBy is ascending on age only: Bob (30) first, then the two 40s in input-relative order (clara before alice). It never looks at names, and it cannot descend age. Previous cousin: R.sortBy.

📈 Practical Patterns

One comparator vs several, prove the source list is intact, then reuse a curried sorter.

Example 3 — vs R.sort with One Comparator

Age descending alone still leaves alice and clara tied. sortWith adds the name rule.

Ramda
const alice = { name: 'alice', age: 40 };
const bob = { name: 'bob', age: 30 };
const clara = { name: 'clara', age: 40 };
const people = [clara, bob, alice];

R.sort(R.descend(R.prop('age')), people);
//=> [clara, alice, bob]

R.sortWith([
  R.descend(R.prop('age')),
  R.ascend(R.prop('name'))
], people);
//=> [alice, clara, bob]
Try It Yourself

How It Works

R.sort with one descend(age) puts the 40s first, then Bob. Clara stays before alice because that was their relative order in [clara, bob, alice]. sortWith then applies name ascending and swaps them. Previous cousin: R.sort.

Example 4 — Original List Unchanged

Confirm the roster is still input order after sortWith. Native Array.sort would have rewritten it.

Ramda
const alice = { name: 'alice', age: 40 };
const bob = { name: 'bob', age: 30 };
const clara = { name: 'clara', age: 40 };
const people = [clara, bob, alice];
const ageNameSort = R.sortWith([
  R.descend(R.prop('age')),
  R.ascend(R.prop('name'))
]);

ageNameSort(people);
//=> [alice, clara, bob]

people;
//=> [clara, bob, alice]
Try It Yourself

How It Works

Capture the return value. The second log is the original array: clara, bob, alice. Native people.sort(...) would have shuffled that same reference.

Example 5 — Curried Reusable Sorter

Fill the comparator list once. Apply the same function to two teams.

Ramda
const byAgeThenName = R.sortWith([
  R.descend(R.prop('age')),
  R.ascend(R.prop('name'))
]);
const teamA = [
  { name: 'clara', age: 40 },
  { name: 'bob', age: 30 },
  { name: 'alice', age: 40 }
];
const teamB = [
  { name: 'zoe', age: 22 },
  { name: 'amy', age: 22 },
  { name: 'ned', age: 50 }
];

byAgeThenName(teamA);
//=> [alice, clara, bob]

byAgeThenName(teamB);
//=> [ned, amy, zoe]
Try It Yourself

How It Works

Data last means byAgeThenName can sit in a pipe or run on many lists. Team B: Ned (50) first, then the two 22s sorted by name (amy before zoe). Same comparator array, two results.

Browser Support

R.sortWith() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.23.0+ · Any JS runtime

Ramda.js R.sortWith()

Load Ramda 0.23.0 or later via CDN or npm, then call R.sortWith with a list of comparators and a list. No extra browser API is required.

100% With Ramda 0.23.0+
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
R.sortWith() Universal

Bottom line: Safe in any project that includes Ramda 0.23.0 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected].

Frequently Asked Questions

R.sortWith(functions, list) returns a new array ordered by a list of comparator functions. Type: [(a, a) → Number] → [a] → [a]. Arity 2, curried. Added in v0.23.0. The first comparator that returns a non-zero value wins; later ones break ties. The original list is not mutated.
people is [clara, bob, alice] with ages 40, 30, 40. ageNameSort = R.sortWith([R.descend(R.prop('age')), R.ascend(R.prop('name'))]) yields [alice, clara, bob]: age descending first, then name ascending on the 40/40 tie.
sortBy takes one key function and sorts that key ascending only. sort takes one comparator. sortWith takes an array of comparators so you can mix directions and break ties. Comparators still return negative / 0 / positive like Array.sort.
No. You get a new list. Native Array.prototype.sort mutates in place. After R.sortWith(comparators, people), people is still the original order.
Use R.ascend(fn) and R.descend(fn) from a key, or write (a, b) => a.score - b.score yourself. Put them in an array in priority order. See also R.ascend and R.descend.
No. R.sortWith is a stable Ramda relation helper from v0.23.0. It is not deprecated, experimental, or non-standard.

Continue to R.symmetricDifference()

Either-not-both of two lists as a unique set — leftovers from the first list, then leftovers from the second.

R.symmetricDifference() →

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