[Solved] Correct bubble sort in JS doesn’t do anything


It should be:

[array[j], array[j + 1]] = [array[j + 1], array[j]];

instead of:

[array[j], array[j + 1] = array[j + 1], array[j]];

Pay close attention to where the square brackets begin and end.


Your current code:

  1. assigns one array index to itself
  2. creates an array with 3 elements
  3. and throws it away immediately

What you actually want is a destructuring assignment. (thanks to @Reyno for the link!)

solved Correct bubble sort in JS doesn’t do anything