not able to sort array object according to given array can you please help me out!!
const data = [ {
parent_email: '[email protected]',
childSurName: null,
childFirstName: null,
id: 22},
{
parent_email: '[email protected]',
childSurName: null,
childFirstName: 'Sachidanand',
id: 31},
{
parent_email: '[email protected]',
childSurName: null,
childFirstName: null,
id: 26}];
const order = [ 31,26 ];
const sorted = data.sort((a, b) => (
order.indexOf(a.id) - order.indexOf(b.id)
));
console.log(sorted)
I need sorted result like my second array are and then after other data :
[ {
parent_email: '[email protected]',
childSurName: null,
childFirstName: 'Sachidanand',
id: 31},
{
parent_email: '[email protected]',
childSurName: null,
childFirstName: null,
id: 26}
{
parent_email: '[email protected]',
childSurName: null,
childFirstName: null,
id: 22},
];
but i am getting below result not getting proper sorted data:
[{
parent_email:"[email protected]",
childSurName:null,
childFirstName:null,
id:22
},
{
parent_email:"[email protected]",
childSurName:null,
childFirstName:"Sachidanand",
id:31
},
{
parent_email:"[email protected]",
childSurName:null,
childFirstName:null,
id:26
}]
"22" !== 22so yourindexOfdoesn't find the IDs