I'm new to Typescript/JavaScript so I'm sure I'm missing something easy.
I have an array of 2 types, each of which has a sub-array of 3 - 4 names:
myArray = [
{
"type": "abc",
"names": {
"Jane Smith": 5,
"John Doe": 3,
"Jack Jones": 2
}
},
{
"type": "xyz",
"names": {
"Jane Smith": 3,
"John Doe": 7,
"Jack Jones": 3
}
}
];
I need to know which main type has the highest overall total and show that first. In this example "xyz" has the highest total ((3 + 7 + 3) vs. (5 + 3 + 2)). So I'd want to display xyz before abc.
I am successfully getting the totals of the two and putting them into a new array called totalValues:
totalValues = [
{
"type": "xyz",
"count": 13
}
},
{
"type": "abc",
"count": 10
}
}
];
The problem is that I need to sort my original array (myArray) by type based on the total and I can't figure it out.
let sortedTotalValues = totalValues.sort().reverse(); //highest value first
let sortedGroupNames = null;
sortedGroupNames = myArray.sort((a, b) => {
return sortedTotalValues[0].indexOf(a) - sortedTotalValues[0].indexOf(b);
});
Any help would be much appreciated.