my question is simple but I haven't found an answer yet.
I have this array:
myArray = [
{
id: '123',
color: 'red'
},
{
id: '234',
color: 'blue'
},
{
id: '345',
color: 'red'
},
{
id: '456',
color: 'yellow'
}
]
I'd like to order this array to have the ones with "color: 'red'" at first and keep the other ones in the same order. I want it to be:
myArray = [
{
id: '123',
color: 'red'
},
{
id: '345',
color: 'red'
},
{
id: '234',
color: 'blue'
},
{
id: '456',
color: 'yellow'
}
]
I tried using Angular's $filter and found a way to do it like this:
$filter('orderBy')(myArray, function(records) {
return records.color !== 'red';
});
I found this solution just playing around but I don't really understand why this works. I believe there should be a better and understandable solution.
Thanks!