main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | import _curry2 from "./internal/_curry2.js";
|
---|
| 2 | /**
|
---|
| 3 | * Sorts a list according to a list of comparators.
|
---|
| 4 | *
|
---|
| 5 | * @func
|
---|
| 6 | * @memberOf R
|
---|
| 7 | * @since v0.23.0
|
---|
| 8 | * @category Relation
|
---|
| 9 | * @sig [(a, a) -> Number] -> [a] -> [a]
|
---|
| 10 | * @param {Array} functions A list of comparator functions.
|
---|
| 11 | * @param {Array} list The list to sort.
|
---|
| 12 | * @return {Array} A new list sorted according to the comarator functions.
|
---|
| 13 | * @see R.ascend, R.descend
|
---|
| 14 | * @example
|
---|
| 15 | *
|
---|
| 16 | * const alice = {
|
---|
| 17 | * name: 'alice',
|
---|
| 18 | * age: 40
|
---|
| 19 | * };
|
---|
| 20 | * const bob = {
|
---|
| 21 | * name: 'bob',
|
---|
| 22 | * age: 30
|
---|
| 23 | * };
|
---|
| 24 | * const clara = {
|
---|
| 25 | * name: 'clara',
|
---|
| 26 | * age: 40
|
---|
| 27 | * };
|
---|
| 28 | * const people = [clara, bob, alice];
|
---|
| 29 | * const ageNameSort = R.sortWith([
|
---|
| 30 | * R.descend(R.prop('age')),
|
---|
| 31 | * R.ascend(R.prop('name'))
|
---|
| 32 | * ]);
|
---|
| 33 | * ageNameSort(people); //=> [alice, clara, bob]
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | var sortWith =
|
---|
| 37 | /*#__PURE__*/
|
---|
| 38 | _curry2(function sortWith(fns, list) {
|
---|
| 39 | return Array.prototype.slice.call(list, 0).sort(function (a, b) {
|
---|
| 40 | var result = 0;
|
---|
| 41 | var i = 0;
|
---|
| 42 |
|
---|
| 43 | while (result === 0 && i < fns.length) {
|
---|
| 44 | result = fns[i](a, b);
|
---|
| 45 | i += 1;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | return result;
|
---|
| 49 | });
|
---|
| 50 | });
|
---|
| 51 |
|
---|
| 52 | export default sortWith; |
---|
Note:
See
TracBrowser
for help on using the repository browser.