1 |
|
2 |
|
3 |
|
4 | ## just-sort-by
|
5 |
|
6 | Part of a [library](https://anguscroll.com/just) of zero-dependency npm modules that do just do one thing.
|
7 | Guilt-free utilities for every occasion.
|
8 |
|
9 | [`🍦 Try it`](https://anguscroll.com/just/just-sort-by)
|
10 |
|
11 | ```shell
|
12 | npm install just-sort-by
|
13 | ```
|
14 | ```shell
|
15 | yarn add just-sort-by
|
16 | ```
|
17 |
|
18 | Produces a new array, sorted in ascending order
|
19 |
|
20 | ```js
|
21 | import sortBy from 'just-sort-by';
|
22 |
|
23 | sortBy([10, 1, 5, 20, 15, 35, 30, 6, 8]); // [1, 5, 6, 8, 10, 15, 20, 30, 35]
|
24 |
|
25 | sortBy([
|
26 | {user: 'fabio', details: {city: "Milan", age: 34}},
|
27 | {user: 'max', details: {city: "Munich", age: 29}},
|
28 | {user: 'zacarias', details: {city: "Sao Paulo", age: 44}},
|
29 | {user: 'robert', details: {city: "Manchester", age: 28}},
|
30 | {user: 'klaus', details: {city: "Zurich", age: 38}},
|
31 | ], function(o) {
|
32 | return o.details.age;
|
33 | });
|
34 |
|
35 | /*
|
36 | [
|
37 | {user: 'robert', age: 28},
|
38 | {user: 'max', age: 29},
|
39 | {user: 'fabio', age: 34},
|
40 | {user: 'klaus', age: 38},
|
41 | {user: 'zacarias', age: 44},
|
42 | ]
|
43 | */
|
44 |
|
45 | sortBy([
|
46 | {user: 'fabio', age: 34},
|
47 | {user: 'max', age: 29},
|
48 | {user: 'zacarias', age: 44},
|
49 | {user: 'robert', age: 28},
|
50 | {user: 'klaus', age: 38},
|
51 | ], 'user');
|
52 | /*
|
53 | [
|
54 | {user: 'fabio', age: 34},
|
55 | {user: 'klaus', age: 38},
|
56 | {user: 'max', age: 29},
|
57 | {user: 'robert', age: 28},
|
58 | {user: 'zacarias', age: 44},
|
59 | ]
|
60 | */
|
61 | ```
|