Question about sorting 2D array

Lets say I have 2D array(example will be from one of the code prep challenges).

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

I want to sort it alphabetically, by the elements with indexes [1](or I can say strings).
In my research I’ve run into some snippets like this:

function sortFunction(a, b) {
    if (a[0] === b[0]) {
        return 0;
    }
    else {
        return (a[0] < b[0]) ? -1 : 1;
    }
}

Here they are doing this check:

    if (a[0] === b[0]) {
        return 0;
    }

However I was able to do the same for Inventory Update challenge and pass the tests with sorting like below:

testArr.sort((a, b) => {
    return (a[1] < b[1]) ? -1 : 1;
  }
  );

So I skipped check if equal
For now I don’t know, is my solution is good for this kind of tasks in general, or I was just lucky with this particular challenge.

You really only need to do something like:

return a[0] - b[0]

I changed sorting like below and its failing tests now. Maybe I misunderstood your suggestion? Link to the challenge in the first post

function updateInventory(arr1, arr2) {
    for (let newItem of arr2) {
      let isItnewItem = true;
      for (let curItem of arr1) {
        if (curItem[1] === newItem[1]) {
          curItem[0] += newItem[0];
          isItnewItem = false;
          break;
        }
      }
      if (isItnewItem) {
        arr1.push(newItem);
      }
    }
  return arr1.sort((a, b) => {
    return a[1] - b[1];
  }
  );
}

// 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"]
];

updateInventory(curInv, newInv);

You’re probably getting NaN, try using the localeCompare method instead.

1 Like

I am geting Nan because JS is trying to give me some number for the below?

Checked localeCompare, that’s definitely an option, thanks.

'Bowling Ball' - 'Dirty Sock' // this is NaN

A string minus a string is NaN. To compare strings you’d have to go character by character and compare the values of charCodeAt. Or just use localeCompare.

1 Like

Got it.
One more thing:
If I run the below:

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

console.log(testArr.sort())

/*
Output:

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

From it I make conclusion, that 2D array, by default, sorted by elements with index 0. I mean testArr[0][0], testArr[1][0] etc

So here in this example it took numbers and compared them(as a strings).
Am I right about that?

If compareFunction is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.

So, the arrays got stringified and then compared as strings. If you had something with 9 items you’d see that it comes after the 88 bowling balls.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.