You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Sorting a json object based on keys either ascending or descending & even recursively
4
1
5
-
### Latest Changes
6
-
-**sort** now throws an error if not provided with an object
7
-
- added support for Typescript
8
2
9
-
### Example -
10
-
> Note : focus on **keys** not values
11
-
3
+
# json-keys-sort
4
+
[](https://app.travis-ci.com/DawnImpulse/json-keys-sort)[](https://coveralls.io/github/DawnImpulse/json-keys-sort?branch=master)[](https://plant.treeware.earth/dawnimpulse/json-keys-sort)
5
+
> Sorting a json object based on keys either ascending or descending & even recursively
6
+
7
+
### Latest Changes (v2.1.0)
8
+
-**sortAsync** : this is a new function which returns a promise
9
+
10
+
### Installation
11
+
`npm install json-keys-sort`
12
+
or
13
+
`yarn add json-keys-sort`
14
+
15
+
### Example -
16
+
> Note : focus on **keys** not values
12
17
~~~~
13
18
const json = require('json-keys-sort');
14
19
var data = {
15
-
"zoho": 4,
16
-
"drake": 2,
17
-
"yogo": {
18
-
"jout": "3.2",
19
-
"aou": {
20
-
"yoyo": "3.1.3",
21
-
"aalo": "3.1.1",
22
-
"go": "3.1.2"
23
-
}
24
-
},
25
-
"abc": 1
26
-
}
27
-
28
-
json.sort(data,true)
20
+
"zoho": 4,
21
+
"drake": 2,
22
+
"yogo": {
23
+
"jout": "3.2",
24
+
"aou": {
25
+
"yoyo": "3.1.3",
26
+
"aalo": "3.1.1",
27
+
"go": "3.1.2"
28
+
}
29
+
},
30
+
"abc": 1
31
+
}
32
+
33
+
// sync (return sorted data)
34
+
json.sort(data, true)
35
+
36
+
// async (return promise of sorted data)
37
+
json.sortAsync(data, true)
29
38
~~~~
30
-
31
-
Output will be -
39
+
40
+
Output will be -
32
41
~~~~
33
42
{
34
43
"abc": 1,
@@ -42,19 +51,21 @@ Output will be -
42
51
"jout": "3.2"
43
52
},
44
53
"zoho": 4
45
-
}
54
+
}
46
55
~~~~
47
-
48
-
+ function parameters
49
-
- data : the json object to sort
50
-
- sort :
51
-
- true (default) : ascending sort , since it is default you can only call `json.sort(data)`
52
-
- false : descending sort
53
-
> Note : the function assumes that you always provide a json object
54
-
55
-
+ The function work recursively and sort all the inner json objects too.
56
+
57
+
+ function parameters (for both **sort** & **sortAsync**)
58
+
- **data:** the json object to sort
59
+
- **sort:**
60
+
- true (default) : ascending sort , since it is default you can only call `json.sort(data)` or `json.sortAsync(data)`
61
+
- false : descending sort
62
+
63
+
+ The function work recursively and sort all the inner json objects too.
56
64
57
65
### Versions
66
+
+`v2.1.0`
67
+
+ **sortAsync** : this is a new function which returns a promise
68
+
58
69
+`v2.0.0`
59
70
+ New : **100%** code coverage
60
71
+ Improvement : **sort** now throws an error if not provided with an object
@@ -64,7 +75,7 @@ Output will be -
64
75
65
76
+`v1.3.0`
66
77
+ Included support for typescript (added declaration file)
67
-
78
+
68
79
+`v1.2.0`
69
80
+ Improvement : json array support
70
81
@@ -75,31 +86,35 @@ Output will be -
75
86
+`v1.0.1`
76
87
+ Bug Fixed : data null checks & default true fix
77
88
78
-
+`v1.0.0`
89
+
+`v1.0.0`
79
90
+ Initial release - containing basic `sort` function only.
80
91
81
-
> Pull requests are always welcomed (kindly sign commits with GPG keys. **THANKS**)
This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/dawnimpulse/json-keys-sort) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
102
+
103
+
104
+
### License (ISC)
105
+
~~~~
106
+
ISC License
107
+
108
+
Copyright 2021, Saksham Khurana (DawnImpulse)
109
+
110
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
111
+
provided that the above copyright notice and this permission notice appear in all copies.
112
+
113
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
114
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
115
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
116
+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
117
+
OR PERFORMANCE OF THIS SOFTWARE.
103
118
~~~~
104
119
105
120
> Written with [StackEdit](https://stackedit.io/).
0 commit comments