I have tried several answers on SO but nothing seems to work. I am processing some data and placing it onto an array. To do this I have the following
let aggData = [];
for (let i = 0; i < data.length; i++) {
let flag = data[i]['Category'].replace(/[_]/g, " ");
flag = flag.toLowerCase()
.split(' ')
.map((s) => s.charAt(0).toUpperCase() + s.substring(1))
.join(' ');
aggData.push({
"Flag": flag,
"Freq": data[i]['Count']
});
}
console.log( JSON.stringify( aggData ) );
The above outputs the following
[
{"Flag":"Four","Freq":123},
{"Flag":"One","Freq":234},
{"Flag":"Three","Freq":345},
{"Flag":"Two Days","Freq":456}
]
So it looks like it is naturally ordering it based on Alphabetical order. I need it to be in an order I define, more specifically this
let order = ["Three", "One", "Two Days", "Four"];
let arr = aggData.sort(function(a,b) {
return order.indexOf( a.key ) > order.indexOf( b.key );
});
console.log( 'Ordered: ', JSON.stringify( arr ) );
The above is returning the exact same order as before though. How can I get it ordered the way I require?
Thanks
a.Flagand subtract inside the compare function