Sorting Multidimensional Array. Algorithm Challange: "Inventory Update" [Spoliers]

Sorting Multidimensional Array. Algorithm Challange: "Inventory Update" [Spoliers]
0

#1

Hello everyone

I just finished the Advanced Algorithm Scripting challange “Inventory Update”, and I wanted to get some clarification on some code I used.

For me I had no issue getting the final array, but my issue was sorting a multidimensional array at the end. What I ended up doing was taking the product in array 1, and seeing if its in array 2, then combining the inventories if I did find it. Also I would take it out of the second array… If not then I put that item into a new array. Then I pushed what’s left in array 2 into the new array. After that all I had to do was sort in alphabetical order by product name. Here is my code for reference:

function updateInventory(arr1, arr2) {

  //out new arrary
  var updatedArr = [];
  
  //loop through our first array and see if the name of the item is in the second array
  for(var i = 0; i < arr1.length; i++){
    
    //our item's array that we are teting, the item itself, and the quanity variables 
    var itemToTestInArr1 = arr1[i];
    var arr1InvenItem = arr1[i][1];
    var arr1InvenQuanity = arr1[i][0];
    
    //set true or false to false when this loop runs
    var trueOrFalse = false;
    
    //now once we selected our item in our first array, loop through our second array to see if it is in there
    for(var j = 0; j < arr2.length; j++){
      
      //our second items array that we are testing, the items itself, and the quanity variables
      var itemToTestInArr2 = arr2[j];
      var arr2InvenItem = arr2[j][1];
      var arr2InvenQuanity = arr2[j][0];
      
      //if the item is in there, then do the following
      if(itemToTestInArr2.indexOf(arr1InvenItem) === 1) {
        
        //add the quanitys together and set it as the new quanity of the first array
        arr1[i][0] = arr1InvenQuanity + arr2InvenQuanity;
        //push that array of quanity and item into the new array
        updatedArr.push(itemToTestInArr1);
        //set true or false to true cause we found it
        trueOrFalse = true;
        
        //now remove that array element from our second array
        arr2.splice(j, 1);
        //if its not found in the second array continue looking through the next items
      } else {
        continue;
      }
    }
    
    //if true or false stays false that means we did not find the item in array 2,  and we will push it from array 1 into our new array.
    if (trueOrFalse === false) {
      updatedArr.push(itemToTestInArr1);
    }
  }
  
  //whatever is left in array 2 means it was not in array 1, so we can just push it into our new array
  for (var k = 0; k < arr2.length; k++){
    updatedArr.push(arr2[k]);
  }
  
  //now we sort the new array by our product name which is index 1 hince the (1) at the end of the entry.
  var sortedArr = updatedArr.sort((function(index){
    return function(a, b){
      return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
    };
  })(1));
  
  return sortedArr;
  
}

// Example inventory lists
var curInv = [
    [21, "Bowling Ball"],
    [2, "Dirty Sock"],
    [1, "Hair Pin"],
    [5, "Microphone"]
];

var newInv = [
    [2, "Hair Pin"],
    [3, "Half-Eaten Apple"],
    [67, "Bowling Ball"],
    [7, "Toothpaste"]
];

var correctReslut = [
    [88, "Bowling Ball"],
    [2, "Dirty Sock"],
    [3, "Hair Pin"],
    [3, "Half-Eaten Apple"],
    [5, "Microphone"],
    [7, "Toothpaste"]
];

updateInventory(curInv, newInv);

I just need help understanding of the sort functions. I had to search online and found this solution for sorting the nested arrays, but there was no explanation on how it works. Just for reference I am talking about this section:

var sortedArr = updatedArr.sort((function(index){
    return function(a, b){
      return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
    };
  })(1));

Also what does the ? mean in javascript?

Please dumb this down for me a lot because it’s how I learn better, just my personal opinion. I won’t take offense.

Thank you in advance everyone for the help and explanation.


#2

Sort Function :

Javascript’s default sort function sorts according to string Unicode code points. i.e(if comaprefunction not supplied)

apples’, ‘bananas’, ‘cherries’
[1, 10, 2, 21]
10 comes before 2, because it starts with 1, which is before 2.

arr.sort(compareFunction)
If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:

  • If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.
  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a.

I use the sort function frequently in the following format to arrange numbers from small to large:

numbers.sort(function(a, b) {
return a - b;
});

The question mark is a ternary operator


condition ? expr1 : expr2
If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.

Logically it flows like an if / else statement


#3

I blurred out your solution to prevent someone from accidentally stumbling across it when viewing your topic.


#4

Thank You for the response.

Sort has always been a weak point for me. So I am following more now, however still have another question. What is index? I know that index is an argument, but how does the code know what argument needs to be?

If you return updatedArr before sorting it, you get this:

[[88, "Bowling Ball"],
[2, "Dirty Sock"],
[3, "Hair Pin"],
[3, "Half-Eaten Apple"],
[5, "Microphone"],
[7, "Toothpaste"]]

So when the code runs, is it saying index is that whole array, or is it saying index is [88, "Bowling Ball"] then moving on to the next one.

I hope that’s clear, sorry if I used some wrong terminology.

Surprisingly I actually understand the next function now.

Thank You again.


#5

Thanks I should have done that. Will watch out next time.