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)
Call
What 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
Parameter
Description
functions
Required. Array. A list of comparator functions. Each should return a negative number, 0, or a positive number, like Array.sort.
list
Required. Array. The collection to order. Ramda copies before sorting; this array is not mutated.
Return Value
Return Value
Description
Array
A 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.
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.
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.
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.
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.