1

I need to sort 2D array of pairs in JavaScript.

const a = [
  [1, 1],
  [1, 2],
  [1, 3],
  [5, 6]
]
const b = [
  [5, 6],
  [1, 2],
  [1, 3],
  [1, 1]
]
b.sort((c, d) => d[0] + d[1] - c[0] + c[1]);
console.log(b)

function compare(a, b) {
  if (a.length != b.length) return false;
  for (let i = 0; i < b.length; i++)
    for (let j = 0; j < b[i].length; j++)
      if (a[i][j] !== b[i][j]) return false;
  return true;
}
console.log(compare(a, b))

I want to sort array b to be equal to array a. Of course real life array is much longer. This is just an example.

I have written function for comparing, but sort function doesn't work properly. Could you please help me to fix this?

Michael M.
  • 10,486
  • 9
  • 18
  • 34
codproe
  • 95
  • 10

1 Answers1

1

First, d[0] + d[1] - c[0] + c[1] is backwards. You should subtract the elements of d from c, not the other way around. Like this: c[0] + c[1] - d[0] + d[1].

Second, you have an order of operations error. You need to subtract the elements of d from c, but your current code subtracts one element of d and adds the other. You need to distribute the negative sign, just like this: c[0] + c[1] - d[0] - d[1]

const a = [
  [1, 1],
  [1, 2],
  [1, 3],
  [5, 6]
]
const b = [
  [5, 6],
  [1, 2],
  [1, 3],
  [1, 1]
]
b.sort((c, d) => c[0] + c[1] - d[0] - d[1]);
console.log(b)

function compare(a, b) {
  if (a.length != b.length) return false;
  for (let i = 0; i < b.length; i++)
    for (let j = 0; j < b[i].length; j++)
      if (a[i][j] !== b[i][j]) return false;
  return true;
}
console.log(compare(a, b))
Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • thank you very much, I have another question. How could I remove duplicates from this array? Do you have any idea? It would help me a lot – codproe Jan 11 '23 at 02:54
  • A simple way would be to convert it to a `Set` then back to an `Array`. Just like this: `let newArray = [...new Set(oldArray)]` – Michael M. Jan 11 '23 at 02:57
  • I tried it but it doesn't work with this type of 2D array, it works with 1D array – codproe Jan 11 '23 at 02:59
  • @codproe Ah yes, sorry. In that case, I'd just loop over every subarray of the 2d array. Like this: `let newTwoD = oldTwoD.map(arr => [...new Set(arr)])` – Michael M. Jan 11 '23 at 03:00
  • I tried this array [5, 6], [1, 2], [1, 3], [1, 1], [1,1] and result was [5, 6], [1, 2], [1, 3], [1], [1] – codproe Jan 11 '23 at 03:05
  • could you please compile it, do you have any idea to make it [5, 6], [1, 2], [1, 3], [1, 1] after removing duplicates – codproe Jan 11 '23 at 03:07
  • @codproe Sorry, I misunderstood your question. I have an answer now, but I think the code is too long to put in a comment. Would you mind asking a new question so I can post I can post an answer there? – Michael M. Jan 11 '23 at 03:10
  • I can post new question every 90 minutes. Could you post code on CodePen and I will post question on stack overflow after 90 minutes inshallah here's screenshot https://prnt.sc/o_AvxeQVwthE – codproe Jan 11 '23 at 03:20
  • @codproe Actually, I think the answers to [this question](https://stackoverflow.com/questions/20339466/how-to-remove-duplicates-from-a-two-dimensional-array) will answer yours. Try the answers there. – Michael M. Jan 11 '23 at 03:27
  • Here's question posted if you are interested https://stackoverflow.com/questions/75078248/ it is a little bit different – codproe Jan 11 '23 at 04:09