Inventory update help

Hi guys! I hope you can help me with this challenge,

I solved the challenge partially I converted 2d array in two objects current Inventory and New Inventory,
then I compared current inventory with new inventory and updated it if the property is in current inventory otherwise I add a property and its value.

Then I converted Object to a 2d array and It does the job, the issue is that I am not sure how to sort a 2d array alphabetically. I am wondering can I even sort object properties alphabetically or I need to sort it as array?

My output right now is:

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

Expected output or correct output should be:

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

Thanks for help everyone, I appreciate your time and effort,
Happy coding!

I figured it out in case someone is struggling with the issue here is how I solved it:
I don’t know how to sort the 2D array alphabetically but I figured out how to sort object alphabetically.
Javascript objects are by definition unsorted, so naturally there is no built-in way of doing this. All browsers, however, keep objects in the order that properties were added as far as I know.
What I did is wrote the function that pushes Object keys in the new array. and then sorted the array. Then I created new object, add a new key n the object for each element in the sorted array and assigned it value from old object current Inventory:

Here is code

// once I updated current inventory with the new inventory this is what I got
currInv = {
Bowling Ball:88
Dirty Sock:2
Hair Pin:3
Half-Eaten Apple:3
Toothpaste	:7
var sortObj = function(obj) {
      //create temporary array
      var temp_array = [];
      //push object keys in the array
      for (var key in obj) {
      // sort the temporary array
      //create empty temporary Obj
      var temp_obj = {};
      //loop through sorted array
      for (var i=0; i<temp_array.length; i++) {
        //add new key (current element of sorted array) and assign it value of  the same key in Object(current Inventory)
        temp_obj[temp_array[i]] = obj[temp_array[i]];
      return temp_obj;
    currInv = sortObj(currInv);

Now I have Object with keys in alphabetical order. Now I can easily push keys and values in new array and create 2d array that is expected as output.
Let me know what do you guys think about this approach and what would be solution to sort 2d array to save memory space and escape so much data type converting.

You can use .sort() function:

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

arr.sort(function(a, b) {
    if (a[1] < b[1]) {
        return -1;
    return 1;


or an even more concise sort:

arr1.sort((a, b) => a[1] > b[1]);
1 Like

@ChadKreutzer @jenovs Thank you guys so much for your time and help!