Skip to content

Instantly share code, notes, and snippets.

@simrandotdev
Created May 2, 2022 20:40
Show Gist options
  • Save simrandotdev/dbfd1768589e57c29592cf0dc3d25628 to your computer and use it in GitHub Desktop.
Save simrandotdev/dbfd1768589e57c29592cf0dc3d25628 to your computer and use it in GitHub Desktop.
function sortByProperty(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
/* next line works with strings and numbers,
* and you may want to customize it to your needs
*/
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
const data = {
AU: [
{label: "Sydney", value: 9},
{label: "Melbourne", value: 9},
{label: "Brisbane", value: 9}
],
IN: [
{label: "Punjab", value: 9},
{label: "Maharashtra", value: 9},
{label: "Tamil Nadu", value: 9}
]
}
const finalOutput = {};
for(var country in data) {
finalOutput[country] = data[country].sort(sortByProperty("label"))
}
console.log(finalOutput);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment