Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Last active March 29, 2020 16:54
Show Gist options
  • Save luislobo14rap/30623e087d54ef269a50ed2e2865db7f to your computer and use it in GitHub Desktop.
Save luislobo14rap/30623e087d54ef269a50ed2e2865db7f to your computer and use it in GitHub Desktop.
sort-json.js
// sort-json.js v1
function sortJson(json, rules) {
let keys = Object.keys(json);
keys = keys.sort(typeof rules == 'function' ? rules() : rules);
let newJson = {};
for (let i = 0; i < keys.length; i++) {
newJson[keys[i]] = json[keys[i]];
};
return newJson;
};
@luislobo14rap
Copy link
Author

luislobo14rap commented Jan 17, 2020

Example: sortJson({a:1, c:3, b:2});
Example 2: sortJson({a:1, c:3, b:2}, naturalSort());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment