0

My code is :

arr1=[2,6,8,56,78,99,100];
arr2=[1,4,6,8,9,99];
arr3=arr1.concat(arr2);
console.log(arr3);
arr3.sort()
console.log(arr3);

But this sort() function gives wrong result. I don't understand why it is giving that result? It should have sorted arr3.

Dumpling
  • 36
  • 5

1 Answers1

2

You have a misunderstanding. Array.sort does not sort numerical, but alphabetical.

You can use this, however:

Array.sort((a, b)  => { return a - b; });
GoldenretriverYT
  • 3,043
  • 1
  • 9
  • 22
  • 1
    Good answer. fyi - you don't need the curlies or the `return` keyword as you are only returning a single value from a single expression. (`.sort((a,b)=>a-b);`) It would also be better to put a working example into a stackblitz so the OP has confidence in your answer. – Randy Casburn Nov 10 '21 at 14:11