1 | import { __, append, identity, useWith } from 'ramda';
|
---|
2 |
|
---|
3 | import 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 |
|
---|
41 | const addValueInAnArray = append(__, []);
|
---|
42 |
|
---|
43 | const sortByProp = useWith(sortByProps, [addValueInAnArray, identity]);
|
---|
44 |
|
---|
45 | export default sortByProp;
|
---|