1 | import { ascend, identity, map, path, pipe, sortWith, useWith } from 'ramda';
|
---|
2 |
|
---|
3 | const pathToAscendSort = pipe(path, ascend);
|
---|
4 | const mapPathsToAscendSort = map(pathToAscendSort);
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Sort a list of objects by a list of paths (if first path value is equivalent, sort by second, etc).
|
---|
8 | *
|
---|
9 | * @func sortByPaths
|
---|
10 | * @memberOf RA
|
---|
11 | * @since {@link https://char0n.github.io/ramda-adjunct/3.1.0|v3.1.0}
|
---|
12 | * @category List
|
---|
13 | * @sig [[k]] -> [{k: v}] -> [{k: v}]
|
---|
14 | * @param {Array.<Array.<string>>} paths A list of paths in the list param to sort by
|
---|
15 | * @param {Array.<object>} list A list of objects to be sorted
|
---|
16 | * @return {Array.<object>} A new list sorted by the paths in the paths param
|
---|
17 | * @example
|
---|
18 | *
|
---|
19 | * const alice = {
|
---|
20 | * name: 'Alice',
|
---|
21 | * address: {
|
---|
22 | * street: 31,
|
---|
23 | * zipCode: 97777,
|
---|
24 | * },
|
---|
25 | * };
|
---|
26 | * const bob = {
|
---|
27 | * name: 'Bob',
|
---|
28 | * address: {
|
---|
29 | * street: 31,
|
---|
30 | * zipCode: 55555,
|
---|
31 | * },
|
---|
32 | * };
|
---|
33 | * const clara = {
|
---|
34 | * name: 'Clara',
|
---|
35 | * address: {
|
---|
36 | * street: 32,
|
---|
37 | * zipCode: 90210,
|
---|
38 | * },
|
---|
39 | * };
|
---|
40 | * const people = [clara, bob, alice]
|
---|
41 | *
|
---|
42 | * RA.sortByPaths([
|
---|
43 | * ['address', 'street'],
|
---|
44 | * ['address', 'zipCode'],
|
---|
45 | * ], people); // => [bob, alice, clara]
|
---|
46 | *
|
---|
47 | * RA.sortByPaths([
|
---|
48 | * ['address', 'zipCode'],
|
---|
49 | * ['address', 'street'],
|
---|
50 | * ], people); // => [bob, clara, alice]
|
---|
51 | */
|
---|
52 |
|
---|
53 | const sortByPaths = useWith(sortWith, [mapPathsToAscendSort, identity]);
|
---|
54 |
|
---|
55 | export default sortByPaths;
|
---|