I have an array containing objects which have a name
and type
.
I am trying to always sort the array so the objects are sorted by type as home
, draw
, away
.
An example of one of the arrays looks like this. The arrays are sent from a backend and are sent in different orders each time. The names are also different each time.
var arr = [
{
type: 'home',
name: 'Liverpool Blues'
}, {
type: 'away',
name: 'Manchester Reds'
},
{
type: 'draw',
name: 'Draw'
}
];
My code looks like this. I thought that draw should get sorted to the middle if home is always pushed to the front, and away is always pushed to the end, although I think there must be an error with how I am sorting the array.
return [...selections].sort((a, b) => {
if (a.type === "HOME") return -1;
if (b.type === "AWAY") return 1;
return 0;
});