if([5,3,4,6,7,8,9,1,2].sort() == [1,2,3,4,5,6,7,8,9]){console.log("It works")}
console.log([1,2,3,4,5,6,7,8,9] == [5,3,4,6,7,8,9,1,2].sort());
Asked
Active
Viewed 27 times
0

Blagoy Simandoff
- 3
- 2
-
Because you are comparing two different objects and this way their reference is compared, which is not the same. See here: https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – Krisztián Balla Mar 15 '20 at 10:31
-
Aside from the comparing arrays thing, it's probably even easier to *check* if an array is sorted, as opposed to comparing it against an already sorted array. – VLAZ Mar 15 '20 at 10:37