Example 1 — Sort Numbers (Official Sample)
Sort numbers from smallest to largest with the diff comparator from the Ramda docs.
const diff = (a, b) => a - b;
const nums = [4, 2, 7, 5];
console.log(R.sort(diff, nums)); // [2, 4, 5, 7]
console.log(nums); // [4, 2, 7, 5] How It Works
- Comparator.
diff(a, b)returnsa - b: negative whenais smaller. - Negative means first. A negative result puts
abeforeb, so small numbers move to the front. - Sorted copy.
R.sortreturns[2, 4, 5, 7]. - No mutation.
numsis still[4, 2, 7, 5].
