1

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; 
    });
}

4 Answers 4

2

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));
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

0

Try sort in lodash

_.sortBy( parent_email, [function(o) { return o.parent_email; }]);

2 Comments

beware, you are not using the array with parent_id
i am just giving info. just change it for Parent_id if you want it
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.