10 examples of 'sort json object by key' in JavaScript

Every line of 'sort json object by key' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
397function sortJSONResults(jsonResults) {
398 var jRes = jsonResults;
399 if(!App.sorted) {
400 jRes.sort(function(a,b) {
401 for(var i = 0; i < a.length; i++) {
402 if(a[i] > b[i]) {
403 return 1;
404 }
405 if(a[i] < b[i]) {
406 return -1;
407 }
408 }
409 return 0;
410 });
411 }
412 return jRes;
413}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

34function sort(a, b) {
35 const first = a.data.order;
36 const second = b.data.order;
37
38 // Ensure the sort returns the same list when there are files with the same `order` value.
39 // Otherwise we might have a different ordering of the TOC.
40 if (first === second) {
41 return JSON.stringify(a).localeCompare(JSON.stringify(b));
42 }
43
44 if (typeof first !== 'number') {
45 return 1;
46 }
47
48 if (typeof second !== 'number') {
49 return -1;
50 }
51
52 return first - second;
53}
8function sort(object) {
9 if (sortArrays === true && Array.isArray(object)) {
10 return object.sort();
11 }
12 else if (typeof object !== "object" || object === null) {
13 return object;
14 }
15
16 return Object.keys(object).sort().map(function (key) {
17 return {
18 key: key,
19 value: sort(object[key])
20 };
21 });
22}
7function sort(object) {
8 if (sortArrays === true && Array.isArray(object)) {
9 return object.sort();
10 }
11 else if (typeof object !== "object" || object === null) {
12 return object;
13 }
14
15 return Object.keys(object).sort().map(function(key) {
16 return {
17 key: key,
18 value: sort(object[key])
19 };
20 });
21}
7function deepSortedJson(jsonObject) {
8 const tmpObj = Object.create(null);
9 const result = [];
10
11 for (const i of Object.keys(jsonObject)) {
12 tmpObj[i] = jsonObject[i];
13 }
14
15 while (true) {
16 const tmpObjkeys = Object.keys(tmpObj);
17
18 if (tmpObjkeys.length === 0) {
19 break;
20 }
21
22 for (const i of tmpObjkeys) {
23 if (tmpObj[i] instanceof Array) {
24 for (const [index, value] of tmpObj[i].entries()) {
25 tmpObj[`${i}[${index}]`] = value;
26 }
27 } else if (tmpObj[i] !== null && typeof tmpObj[i] === 'object') {
28 const keys = Object.keys(tmpObj[i]);
29 if (keys.length === 0) {
30 result.push(`${i}`);
31 } else {
32 for (const key of keys) {
33 tmpObj[`${i}.${key}`] = tmpObj[i][key];
34 }
35 }
36 } else {
37 result.push(`${i}=${tmpObj[i]}`);
38 }
39
40 delete tmpObj[i];
41 }
42 }
43
44 return result.sort();
45}
73function sortObjectByProperties(o) {
74 const tmpString = JSON.stringify(o, Object.keys(o).sort());
75 return JSON.parse(tmpString);
76}
54function sortObject(obj) {
55 const newObject = {};
56 Object.keys(obj)
57 .sort()
58 .forEach(key => {
59 newObject[key] = obj[key];
60 });
61 return newObject;
62}
18export function sortObject<T>(object: any): T {
19 let sortedObj: any = {}
20 let keys = _.keys(object)
21 keys = _.sortBy(keys, (key: string) => {
22 return key
23 })
24
25 _.each(keys, (key: string) => {
26 if (typeof object[key] === 'object' && !(object[key] instanceof Array)) {
27 sortedObj[key] = sortObject(object[key])
28 } else {
29 sortedObj[key] = object[key]
30 }
31 })
32
33 return sortedObj as T
34}
125sortObjectByKeys(obj) {
126 if (!obj || typeof obj !== 'object') {
127 return {};
128 }
129
130 let newObj = {};
131 Object.keys(obj).sort().forEach((k) => {
132 newObj[k] = obj[k];
133 });
134 return newObj;
135}
122function sortObj(obj) {
123 var keys = Object.keys(obj)
124 var o = {}
125 keys.sort()
126 for (var i=0; i<keys.length; i++) {
127 o[keys[i]] = obj[keys[i]]
128 }
129 return o
130}

Related snippets