Last active
March 29, 2020 16:54
-
-
Save luislobo14rap/30623e087d54ef269a50ed2e2865db7f to your computer and use it in GitHub Desktop.
sort-json.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: sortJson({a:1, c:3, b:2});
Example 2: sortJson({a:1, c:3, b:2}, naturalSort());