Ramda’s R.sortBy() answers a ranking question: order this list by a value you compute from each item. You pass a function that produces an ordered key (a number, a string, a date), then the list. Ramda maps that function, sorts the copy by those keys in ascending order, and returns the original items in that new order. Native Array.prototype.sort takes a comparator and rewrites the same array. sortBy is data-last, curried, and leaves the source list alone.
The official samples are the whole idea. const sortByFirstItem = R.sortBy(R.prop(0)); then sortByFirstItem([[-1, 1], [-2, 2], [-3, 3]]) is [[-3, 3], [-2, 2], [-1, 1]]. Names with mixed case: R.sortBy(R.compose(R.toLower, R.prop('name'))) orders Alice, Bob, Clara alphabetically without caring about caps. Need a custom pairwise comparator? That is R.sort. Need several comparators? That is R.sortWith (v0.23.0). Need a ready comparator from a key? R.ascend and R.descend. Previous helper is R.propEq. Next is R.sortWith.
Syntax
Call R.sortBy. Start with this line.
Ramda
R.sortBy(fn, list)
Call
What it does
R.sortBy(fn, list)
Primary signature.
R.sortBy(...)
Call the relation helper.
Prefer import { sortBy } from "ramda" when you want a named import.
Library declarations
Ramda
R.sortBy(fn, list)
Parameters
Parameter
Description
fn
Required. Function. Produces a sort key from each item. Keys should be orderable (numbers, strings, dates).
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 by the keys generated by fn, ascending. Original items, new order.
Ramda Examples
📚 Getting Started
The official Ramda pair sort, then the case-insensitive name sample.
Example 1 — Official sortByFirstItem on Pairs
From the Ramda docs. Project index 0, then sort the pairs by that number.
That is the docs sample from ramdajs.com. Keys are -1, -2, -3. Ascending order puts -3 first. Each result row is still the original pair, not just the first number.
Example 2 — Official Case-Insensitive Name Sort
Compose toLower with prop('name') so ALICE, Bob, and clara sort as alice, bob, clara.
Without toLower, ASCII would put ALICE before Bob before clara already — but mixed case is unreliable. The docs compose lowercase first so the key is always a comparable string. Input order was clara, bob, alice; output is alice, bob, clara.
📈 Practical Patterns
Key function vs comparator, sort objects by age without mutation, then descending.
Example 3 — vs R.sort with a Comparator
Same numbers, two APIs. sortBy projects identity. sort subtracts the pair.
R.identity is “the item is the key.” R.sort wants a comparator like the official List sample. Both copy first, so nums is still [4, 2, 7, 5]. Native nums.sort(diff) would rewrite nums. Previous cousin: R.sort.
Example 4 — Sort Objects by Age with R.prop('age')
Curry the key function. Confirm the original roster is unchanged after the call.
R.prop('age') is the key function. Data last means byAge can sit in a pipe. The second log is the original array: Eve, Ada, Ida in that order. Native people.sort(...) would have shuffled that same reference.
Example 5 — Descending: R.reverse or R.descend
sortBy is ascending only. Reverse the result, or switch to R.sort with R.descend.
Both paths put age 45 first. R.reverse after sortBy is two steps. R.sort(R.descend(R.prop('age')), people) is one comparator. Several keys in order belong to R.sortWith, not a second sortBy.
Compatibility
Browser Support
R.sortBy() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Ramda 0.1.0+ · Any JS runtime
Ramda.js R.sortBy()
Load Ramda 0.1.0 or later via CDN or npm, then call R.sortBy with a key function and a list. No extra browser API is required.
100%With Ramda 0.1.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.sortBy()Universal
Bottom line: Safe in any project that includes Ramda 0.1.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.sortBy(fn, list) returns a new array ordered by the keys fn produces for each item. Type: Ord b => (a → b) → [a] → [a]. Arity 2, curried. Added in v0.1.0. Sort is ascending on those keys. The original list is not mutated.
sortByFirstItem = R.sortBy(R.prop(0)) turns [[-1, 1], [-2, 2], [-3, 3]] into [[-3, 3], [-2, 2], [-1, 1]]. sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name'))) orders people alice, bob, clara by lowercased name.
sortBy takes a key function. sort takes a comparator that returns -, 0, or +. For numbers, R.sortBy(R.identity, nums) matches R.sort((a, b) => a - b, nums). Custom pairwise rules, mixed directions, or several keys need R.sort, R.ascend, R.descend, or R.sortWith.
No. You get a new list. Native Array.prototype.sort mutates in place. After R.sortBy(R.prop('age'), people), people is still the original order.
sortBy is ascending only. Reverse after: R.reverse(R.sortBy(fn, list)). Or skip sortBy and use R.sort(R.descend(fn), list). Several comparators: R.sortWith (v0.23.0).
No. R.sortBy is a stable Ramda relation helper from v0.1.0. It is not deprecated, experimental, or non-standard.