UNPKG

1.24 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4/**
5 * Sorts the list according to the supplied function.
6 *
7 * @func
8 * @memberOf R
9 * @since v0.1.0
10 * @category Relation
11 * @sig Ord b => (a -> b) -> [a] -> [a]
12 * @param {Function} fn
13 * @param {Array} list The list to sort.
14 * @return {Array} A new list sorted by the keys generated by `fn`.
15 * @example
16 *
17 * const sortByFirstItem = R.sortBy(R.prop(0));
18 * const pairs = [[-1, 1], [-2, 2], [-3, 3]];
19 * sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
20 *
21 * const sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
22 * const alice = {
23 * name: 'ALICE',
24 * age: 101
25 * };
26 * const bob = {
27 * name: 'Bob',
28 * age: -10
29 * };
30 * const clara = {
31 * name: 'clara',
32 * age: 314.159
33 * };
34 * const people = [clara, bob, alice];
35 * sortByNameCaseInsensitive(people); //=> [alice, bob, clara]
36 */
37
38
39var sortBy =
40/*#__PURE__*/
41_curry2(function sortBy(fn, list) {
42 return Array.prototype.slice.call(list, 0).sort(function (a, b) {
43 var aa = fn(a);
44 var bb = fn(b);
45 return aa < bb ? -1 : aa > bb ? 1 : 0;
46 });
47});
48
49module.exports = sortBy;
\No newline at end of file