Example 1 — Official: Sort by Function
Rank numbers by Math.sin.
_.sortBy([1, 2, 3, 4, 5, 6], function (num) {
return Math.sin(num);
});
// → [5, 4, 6, 3, 1, 2] How It Works
- Iteratee computes
Math.sin(num)for each value. - Ascending sort by that criterion (not by the number itself).
- Result is a new array:
[5, 4, 6, 3, 1, 2]. - Source unchanged — always assign the return value.

