Reputation: 51
Below given is the image of the code that was run in the console and the corresponding output is also shown.
While applying Sorting in Arrays in JS, how the 010 is converted to 8 and placed at last? whereas as per the rules it should come at the top of the sorted list.
Upvotes: 0
Views: 30
Reputation: 1365
First, "010" is converted to "8" because it is treated as Octal literal, you should not start an integer with a zero. Secondly if you want to sort an array of integers, you should follow this convention:
const arr = [34, 10, 11, 658, 1000]
console.log(arr.sort((a, b) => a - b ))
Upvotes: 1
Reputation: 730
010 is octal literal which gets converted to its equivalent decimal literal which is 8.
Upvotes: 0