-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsortBy.js
37 lines (32 loc) · 1.08 KB
/
sortBy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Example of sortBy() API
---------------------
You need to pass an 'property' parameter first.
You can pass the 'order' parameter to define the ordering.
If you skip the 'order' property the data will be by default
ordered as **ascending**. You need to pass **'desc'** as the
'order' parameter to sort the data in **descending** order.
Also, you can pass a compare function in 'order' parameter
to define your own logic to order the data.
*/
const jsonQ = require('../index.js');
let Q = new jsonQ(__dirname + '/data.json');
const ascSorted = Q.from('products')
.sortBy('price')
.fetch();
console.log('-------- Printing Ascending ordered data ---------');
console.log(ascSorted);
Q = Q.reset();
const descSorted = Q.from('products')
.sortBy('price', 'desc')
.fetch();
console.log('-------- Printing Descending ordered data ---------');
console.log(descSorted);
Q = Q.reset();
const customSorted = Q.from('products')
.sortBy('price', function(a, b) {
return a - b;
})
.fetch();
console.log('-------- Printing Custom ordered data ---------');
console.log(customSorted);