source: node_modules/ramda-adjunct/src/sortByProp.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.2 KB
Line 
1import { __, append, identity, useWith } from 'ramda';
2
3import sortByProps from './sortByProps';
4
5/**
6 * Sort a list of objects by a property.
7 *
8 * @func sortByProp
9 * @memberOf RA
10 * @since {@link https://char0n.github.io/ramda-adjunct/3.4.0|v3.4.0}
11 * @category List
12 * @sig k -> [{k: v}] -> [{k: v}]
13 * @param {Array.<string>} prop The property 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 property in the prop param
16 * @example
17 *
18 * // sorting list of tuples
19 * const sortByFirstItem = sortByProp(0);
20 * const listOfTuples = [[-1, 1], [-2, 2], [-3, 3]];
21 * sortByFirstItem(listOfTuples); // => [[-3, 3], [-2, 2], [-1, 1]]
22 *
23 * // sorting list of objects
24 * const sortByName = sortByProp('name');
25 * const alice = {
26 * name: 'ALICE',
27 * age: 101,
28 * };
29 * const bob = {
30 * name: 'Bob',
31 * age: -10,
32 * };
33 * const clara = {
34 * name: 'clara',
35 * age: 314.159,
36 * };
37 * const people = [clara, bob, alice];
38 * sortByName(people); // => [alice, bob, clara]
39 */
40
41const addValueInAnArray = append(__, []);
42
43const sortByProp = useWith(sortByProps, [addValueInAnArray, identity]);
44
45export default sortByProp;
Note: See TracBrowser for help on using the repository browser.