Created
August 2, 2018 02:16
-
-
Save staxmanade/eddba7349cc6c5aed8c4389ce21a2fc4 to your computer and use it in GitHub Desktop.
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
var recursivelyOrderKeys = function(unordered) { | |
// If it's an array - recursively order any | |
// dictionary items within the array | |
if (Array.isArray(unordered)) { | |
unordered.forEach(function (item, index) { | |
unordered[index] = recursivelyOrderKeys(item); | |
}); | |
return unordered; | |
} | |
// If it's an object - let's order the keys | |
if (typeof unordered === 'object') { | |
var ordered = {}; | |
Object.keys(unordered).sort().forEach(function(key) { | |
ordered[key] = recursivelyOrderKeys(unordered[key]); | |
}); | |
return ordered; | |
} | |
return unordered; | |
}; | |
var stringifyKeysInOrder = function(data) { | |
var sortedData = recursivelyOrderKeys(data); | |
return JSON.stringify(sortedData, null, ' '); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment