1var sortBy = require('sort-by'),
2 users = [];
3
4users = [{
5 id: 7,
6 name: 'Foo',
7 age: '34',
8 email: { primary: 'foo@email.com' }
9}, {
10 id: 3,
11 name: 'Baz',
12 age: '67',
13 email: { primary: 'baz@email.com' }
14}, {
15 id: 4,
16 name: 'Bar',
17 age: '67',
18 email: { primary: 'bar@email.com' }
19}];
20
21users.sort(sortBy('name', 'age'));
22
23/**
24* result:
25* [{id: 4, name: 'Bar', age: '67', email: { primary: 'bar@email.com' }},
26* {id: 3, name: 'Baz', age: '67', email: { primary: 'baz@email.com' }},
27* {id: 7, name: 'Foo', age: '34', email: { primary: 'foo@email.com' }}]
28*/
29
30/**
31* Use `-` to reverse the sort order
32*/
33
34users.sort(sortBy('-id', 'name'));
35
36/*
37* result:
38* [{id: 7, name: 'Foo', age: '34', email: { primary: 'foo@email.com' }},
39* {id: 4, name: 'Bar', age: '67', email: { primary: 'bar@email.com' }},
40* {id: 3, name: 'Baz', age: '67', email: { primary: 'baz@email.com' }}]
41*/
42
43/**
44* Use `.` notation to traverse nested properties. See [object-path](https://www.npmjs.org/package/object-path) npm module for support.
45*/
46
47users.sort(sortBy('age', 'email.primary'));
48
49/*
50* result:
51* [{id: 7, name: 'Foo', age: '34', email: { primary: 'foo@email.com' }},
52* {id: 4, name: 'Bar', age: '67', email: { primary: 'bar@email.com' }},
53* {id: 3, name: 'Baz', age: '67', email: { primary: 'baz@email.com' }}]
54*/
55