Created
May 2, 2022 20:40
-
-
Save simrandotdev/dbfd1768589e57c29592cf0dc3d25628 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
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