Ramda.js R.sortBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Relation

Definition

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)
CallWhat 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

ParameterDescription
fnRequired. Function. Produces a sort key from each item. Keys should be orderable (numbers, strings, dates).
listRequired. Array. The collection to order. Ramda copies before sorting; this array is not mutated.

Return Value

Return ValueDescription
ArrayA 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.

Ramda
const sortByFirstItem = R.sortBy(R.prop(0));
const pairs = [[-1, 1], [-2, 2], [-3, 3]];

sortByFirstItem(pairs);
//=> [[-3, 3], [-2, 2], [-1, 1]]
Try It Yourself

How It Works

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.

Ramda
const sortByNameCaseInsensitive = R.sortBy(
  R.compose(R.toLower, R.prop('name'))
);
const alice = { name: 'ALICE', age: 101 };
const bob = { name: 'Bob', age: -10 };
const clara = { name: 'clara', age: 314.159 };
const people = [clara, bob, alice];

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

How It Works

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.

Ramda
const nums = [4, 2, 7, 5];
const diff = function (a, b) { return a - b; };

R.sortBy(R.identity, nums);
//=> [2, 4, 5, 7]

R.sort(diff, nums);
//=> [2, 4, 5, 7]

nums;
//=> [4, 2, 7, 5]
Try It Yourself

How It Works

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.

Ramda
const people = [
  { name: 'Eve', age: 32 },
  { name: 'Ada', age: 21 },
  { name: 'Ida', age: 45 }
];
const byAge = R.sortBy(R.prop('age'));

byAge(people);
//=> [{ name: 'Ada', age: 21 }, { name: 'Eve', age: 32 }, { name: 'Ida', age: 45 }]

people;
//=> [{ name: 'Eve', age: 32 }, { name: 'Ada', age: 21 }, { name: 'Ida', age: 45 }]
Try It Yourself

How It Works

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.

Ramda
const people = [
  { name: 'Eve', age: 32 },
  { name: 'Ada', age: 21 },
  { name: 'Ida', age: 45 }
];

R.reverse(R.sortBy(R.prop('age'), people));
//=> [Ida, Eve, Ada]

R.sort(R.descend(R.prop('age')), people);
//=> [Ida, Eve, Ada]
Try It Yourself

How It Works

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.

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 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.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.

Continue to R.sortWith()

Stack several comparators in priority order — mix R.ascend and R.descend, and break ties after R.sortBy.

R.sortWith() →

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