Reputation: 146
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;
});
}
Upvotes: 1
Views: 88
Reputation: 197
const data = [{
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
}
]
const orderBy = [{
parent_id: '[email protected]'
},
{
parent_id: '[email protected]'
},
{
parent_id: '[email protected]'
}
]
const length = data.length
const dataOrders = orderBy.reduce((result, item, index) => (result[item.parent_id] = index + 1) && result, {})
const dataOrdered = data.sort((a, b) => (dataOrders[a.parent_email] || length) - (dataOrders[b.parent_email] || length))
console.log(dataOrdered)
Upvotes: 0
Reputation: 15530
To me it seems not exactly like sorting (as you grab matching items in desired order and don't care about the order of the rest), so you may go, like (though, it may produce unexpected output in case of duplicating e-mails):
const src = [
{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}
];
const order = [
{parent_id: '[email protected]'},
{parent_id: '[email protected]'},
{parent_id: '[email protected]'}
];
const res = order
.map(item => src.find(entry => entry.parent_email == item.parent_id))
.concat(src.filter(entry => !order.map(item => item.parent_id).includes(entry.parent_email)));
console.log(res);
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}
Upvotes: 0
Reputation: 1569
Try sort in lodash
_.sortBy( parent_email, [function(o) { return o.parent_email; }]);
Upvotes: 0
Reputation: 138277
You should sort the first array by the index in the second array:
const emailRank = new Map();
for(const [index, { parent_id }] of array2.entries())
emailRank.set(parent_id, index);
array1.sort((a, b) => emailRank.get(b.parent_email) - emailRank.get(a.parentEmail));
Upvotes: 2