source: node_modules/ramda-adjunct/src/sortByPaths.js

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.4 KB
RevLine 
[d24f17c]1import { ascend, identity, map, path, pipe, sortWith, useWith } from 'ramda';
2
3const pathToAscendSort = pipe(path, ascend);
4const 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
53const sortByPaths = useWith(sortWith, [mapPathsToAscendSort, identity]);
54
55export default sortByPaths;
Note: See TracBrowser for help on using the repository browser.