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
|
Line | |
---|
1 | import _curry2 from "./internal/_curry2.js";
|
---|
2 | /**
|
---|
3 | * Sorts the list according to the supplied function.
|
---|
4 | *
|
---|
5 | * @func
|
---|
6 | * @memberOf R
|
---|
7 | * @since v0.1.0
|
---|
8 | * @category Relation
|
---|
9 | * @sig Ord b => (a -> b) -> [a] -> [a]
|
---|
10 | * @param {Function} fn
|
---|
11 | * @param {Array} list The list to sort.
|
---|
12 | * @return {Array} A new list sorted by the keys generated by `fn`.
|
---|
13 | * @example
|
---|
14 | *
|
---|
15 | * const sortByFirstItem = R.sortBy(R.prop(0));
|
---|
16 | * const pairs = [[-1, 1], [-2, 2], [-3, 3]];
|
---|
17 | * sortByFirstItem(pairs); //=> [[-3, 3], [-2, 2], [-1, 1]]
|
---|
18 | *
|
---|
19 | * const sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
|
---|
20 | * const alice = {
|
---|
21 | * name: 'ALICE',
|
---|
22 | * age: 101
|
---|
23 | * };
|
---|
24 | * const bob = {
|
---|
25 | * name: 'Bob',
|
---|
26 | * age: -10
|
---|
27 | * };
|
---|
28 | * const clara = {
|
---|
29 | * name: 'clara',
|
---|
30 | * age: 314.159
|
---|
31 | * };
|
---|
32 | * const people = [clara, bob, alice];
|
---|
33 | * sortByNameCaseInsensitive(people); //=> [alice, bob, clara]
|
---|
34 | */
|
---|
35 |
|
---|
36 | var sortBy =
|
---|
37 | /*#__PURE__*/
|
---|
38 | _curry2(function sortBy(fn, list) {
|
---|
39 | return Array.prototype.slice.call(list, 0).sort(function (a, b) {
|
---|
40 | var aa = fn(a);
|
---|
41 | var bb = fn(b);
|
---|
42 | return aa < bb ? -1 : aa > bb ? 1 : 0;
|
---|
43 | });
|
---|
44 | });
|
---|
45 |
|
---|
46 | export default sortBy; |
---|
Note:
See
TracBrowser
for help on using the repository browser.