5

I have an array of objects:

var items = [
   {
      "id":"sugar",
      "type": 'eatables'
   },
   {
      "id":"petrol",
      "type": 'utility'
   },
   {
      "id":"apple",
      "type": 'fruits'
   },
   {
      "id":"mango",
      "type": 'fruits'
   },
   {
      "id":"book",
      "type": 'education'
   }
];

Now I have another array of orders with the help of which I want to sort items array:

var orders = [
   {
      "id":"sugar",
      "order":5
   },
   {
      "id":"book",
      "order":1
   }
];

Now what I am trying so far in my logic is that I am putting so many loops that it is totally creating mess.

Can anyone suggest me with a short and optimized logic for this?

2
  • What happens with the other elements of the array ? Commented Nov 21, 2020 at 13:20
  • @MihaiAlexandru-Ionut they remains in the same sequence as before Commented Nov 21, 2020 at 13:20

2 Answers 2

4

One approach could be creating one dictionary which will keep the order for every element. Also, I've iterated the whole items array to store the position for the elements that are not in the orders array.

First of all, I'll declare one array which keep the whole orders, thus one array with 1..N elements.

var orderNumbers = Array.from({length: items.length}, (_, v) => v + 1);

Then I started to create the dictionary by iterating orders array and remove orders from orderNumbers.

The last step is iterating the items array and use shift method to "pop" the first element.

The final dictionary will look like

{
  "sugar": 2,
  "book": 3,
  "petrol": 1,
  "apple": 4,
  "mango": 5
}

In this code I used one dictionary because its lookup has complexity of O(1).

var items = [ { "id":"sugar", "type": 'eatables' }, { "id":"petrol", "type": 'utility' }, { "id":"apple", "type": 'fruits' }, { "id":"mango", "type": 'fruits' }, { "id":"book", "type": 'education' } ], orders = [ { "id":"sugar", "order":2 }, { "id":"book", "order":3 } ], orderNumbers = Array.from({length: items.length}, (_, v) => v + 1);

var ordersDict = orders.reduce((acc, item) => { 
     acc[item.id] = item.order;
     
     //remove from order numbers
     let index = orderNumbers.findIndex(el => el == item.order);
     orderNumbers.splice(index, 1);
     
     return acc;
}, {});

for(let i = 0; i < items.length; i++){
  if(!ordersDict.hasOwnProperty(items[i].id)){
    ordersDict[items[i].id] = orderNumbers[0];
    orderNumbers.shift();
   }
}

//sort the array
items.sort((a,b) => ordersDict[a.id] - ordersDict[b.id]);

console.log(items);

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah now its working perfect. I am accepting your answer. But can we do something more to optimize the logic or reduce the line of code and stuff...
@StormTrooper, I have updated my answer. That's why I used a dictionary, because the lookup has complexity of O(1) and I tried to avoid find function everytime.
0
let oorder = new Object();

orders.map(item=>{oorder[item.id]=item.order});

var new_items = [];

items.map(item=>{new_items[oorder[item.id]-1]=item});

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.