I have an array elements which need to sort and make selected element into top of array.
[{
parent_email: '[email protected]',
id: 143,
unreadCount: 0
},
{
parent_email: '[email protected]',
id: 210,
unreadCount: 0
},
{
parent_email: '[email protected]',
id: 225,
unreadCount: 0
},
{
parent_email: '[email protected]',
id: 221,
unreadCount: 0
},
{
parent_email: '[email protected]',
id: 224,
unreadCount: 0
}]
i have another array by which above array element need to sort. first element is on top second is on second position third is on third position and so on.
[{
parent_id: '[email protected]'
},
{
parent_id: '[email protected]'
},
{
parent_id: '[email protected]'
}]
my result array should be like
[{
parent_email: '[email protected]',
id: 221,
unreadCount: 0
},
{
parent_email: '[email protected]',
id: 225,
unreadCount: 0
},
{
parent_email: '[email protected]',
id: 210,
unreadCount: 0
},
{
parent_email: '[email protected]',
id: 143,
unreadCount: 0
},
{
parent_email: '[email protected]',
id: 224,
unreadCount: 0
}]
i have tried but it only sort single element not more then one.
for (var i = array2.length - 1; i >= 0; i--) {
array1.sort(function(x,y){
return x.parent_email == rows[i].parent_id ? -1 : y.parent_email == rows[i].parent_id ? 1 : 0;
});
}