Example 1 — Official byAge Sort
Youngest first — the sample from the Ramda docs.
const byAge = R.comparator((a, b) => a.age < b.age);
const people = [
{ name: 'Emma', age: 70 },
{ name: 'Peter', age: 78 },
{ name: 'Mikhail', age: 62 }
];
console.log(R.sort(byAge, people)); How It Works
- Write the less-than test.
(a, b) => a.age < b.ageanswers “is a younger than b?” - Wrap with
R.comparator. That predicate becomes a-1 / 0 / 1function namedbyAge. - Sort immutably.
R.sort(byAge, people)returns Mikhail (62), Emma (70), Peter (78). - Source stays intact.
peopleis unchanged — only the returned array is ordered.
