Skip to content

Instantly share code, notes, and snippets.

@FallenMax
Created October 25, 2021 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FallenMax/e2cea2d860914fb3956e7684a8d13d60 to your computer and use it in GitHub Desktop.
Save FallenMax/e2cea2d860914fb3956e7684a8d13d60 to your computer and use it in GitHub Desktop.
sort json
{
const sort = (json)=>{
if(json == null) return null
if(Array.isArray(json)) return json.map(sort)
if(typeof json === 'object') {
let sorted = {}
Object.keys(json).sort().forEach(key => {
sorted[key] = sort(json[key])
})
return sorted
}
return json
}
if (!window.json) {
console.info('window.json not found');
} else {
window.json_sorted = sort(window.json)
console.info('sorted at window.json_sorted (copied):', window.json_sorted)
copy(window.json_sorted)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment