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