Created
March 16, 2020 14:04
-
-
Save rajatk16/cb7c643c08b1d4d09af6620835654ec1 to your computer and use it in GitHub Desktop.
This file contains 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
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