I have list of objects in the following structure which are already in sorted order by name property in the top level.
[{
name: 'name1'
team: 'team1'
statuses: [{ time: 'day1', color: 'green', message: 'looks good'}, { time: 'day2', color: 'green', message: 'looks good'}]
},
{
name: 'name2'
team: 'team2'
statuses: [{ time: 'day1', color: 'yellow', message: 'mild concern'}, { time: 'day2', color: 'red', message: 'critical issue'}]
},
{
name: 'name3'
team: 'team3'
statuses: [{ time: 'day1', color: 'orange', message: 'mild concern'}, { time: 'day2', color: 'orange', message: 'potential issue'}]
}]
The above list should be sorted with custom sort order(red, orange, green) based on color property of last object in the status list. Expected list contains objects in this order team2, team3, team1, if there are multiple of same color then it should retain sorted of name property at top level.
I tried using reduce function in the following way and combined all of them together, but not getting expected output.
teams.reduce((r, t) => {
if(t.statuses[1].color === 'red');
r.push(t)
return r;
}, { [] })
teams.reduce((r, t) => {
if(t.statuses[1].color === 'orange');
r.push(t)
return r;
}, { [] })
teams.reduce((r, t) => {
if(t.statuses[1].color === 'green');
r.push(t)
return r;
}, { [] })