I have an array of objects that need to be sorted into a list but I'd like to group tied values. The array is essentially:
var sorted = [
{
section:"Red",
score:12
},
{
section:"Green",
score:12
},
{
section:"Blue",
score:9
},
...
]
And the result I am aiming for would be to output something along the lines of Your first choices are Red and Green, your second choice is Blue, your third choices are...
I've sorted the main array already to sort the objects in an ascending order, but don't really know how to address the objects that have the same score.
I have been trying to get this to work https://www.tutorialspoint.com/sort-the-array-and-group-all-the-identical-duplicate-numbers-into-their-separate-subarray-in-javascript but I can't quite figure it out to adapt to my situation. This doesn't quite work but I am getting close. It seems to be missing the first result...
var sorted = [
{
section:"Red",
score:12
},
{
section:"Green",
score:12
},
{
section:"Blue",
score:9
},
{
section:"Yellow",
score:8
},
{
section:"Grey",
score:6
},
{
section:"Black",
score:6
}
]
const sortAndGroup = (sorted = []) => {
let result = [];
let groupArray = [];
for (let i = 1; i < sorted.length; i++) {
if (sorted[i - 1].score !== sorted[i].score) {
groupArray = [];
result.push(groupArray);
};
groupArray.push(sorted[i].section + ", " + sorted[i].score);
};
return result;
};
console.log(sortAndGroup(sorted));