Skip to content

Instantly share code, notes, and snippets.

@rajatk16
Created March 16, 2020 14:04
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 rajatk16/cb7c643c08b1d4d09af6620835654ec1 to your computer and use it in GitHub Desktop.
Save rajatk16/cb7c643c08b1d4d09af6620835654ec1 to your computer and use it in GitHub Desktop.
const data = {
"people":[
{
"f_name":"john",
"l_name":"doe",
"sequence":"0",
"title":"president",
"url":"google.com",
"color":"333333"
},
{
"f_name":"michael",
"l_name":"goodyear",
"sequence":"0",
"title":"general manager",
"url":"google.com",
"color":"333333"
}
]
}
const people = data.people
const sortJSON = (key, order) => {
return people.sort((a,b) => {
if(order === 'asc') {
return (a[key] > b[key]) ? 1 : ((a[key] < b[key]) ? -1 : 0);
} else {
return (b[key] > a[key]) ? 1 : ((b[key] < a[key]) ? -1 : 0);
}
})
}
sortJSON('title', 'asc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment