UNPKG

1.2 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4/**
5 * Sorts a list according to a list of comparators.
6 *
7 * @func
8 * @memberOf R
9 * @since v0.23.0
10 * @category Relation
11 * @sig [(a, a) -> Number] -> [a] -> [a]
12 * @param {Array} functions A list of comparator functions.
13 * @param {Array} list The list to sort.
14 * @return {Array} A new list sorted according to the comarator functions.
15 * @example
16 *
17 * const alice = {
18 * name: 'alice',
19 * age: 40
20 * };
21 * const bob = {
22 * name: 'bob',
23 * age: 30
24 * };
25 * const clara = {
26 * name: 'clara',
27 * age: 40
28 * };
29 * const people = [clara, bob, alice];
30 * const ageNameSort = R.sortWith([
31 * R.descend(R.prop('age')),
32 * R.ascend(R.prop('name'))
33 * ]);
34 * ageNameSort(people); //=> [alice, clara, bob]
35 */
36
37
38var sortWith =
39/*#__PURE__*/
40_curry2(function sortWith(fns, list) {
41 return Array.prototype.slice.call(list, 0).sort(function (a, b) {
42 var result = 0;
43 var i = 0;
44
45 while (result === 0 && i < fns.length) {
46 result = fns[i](a, b);
47 i += 1;
48 }
49
50 return result;
51 });
52});
53
54module.exports = sortWith;
\No newline at end of file