This isn't specific to the sort. If you just type 022
into a console, you'll get back 18. This is because 022 is being interpreted as an OctalIntegerLiteral
, instead of as a DecimalLiteral
. This is not always the case however. Taking a look at the documentation:
Note that decimal literals can start with a zero (0) followed by another decimal digit, but If all digits after the leading 0 are smaller than 8, the number is interpreted as an octal number. This won't throw in JavaScript, see bug 957513. See also the page about parseInt().
EDIT: To remove the leading 0s and interpret the 022
as a decimal integer, you can use parseInt
and specify the base:
parseInt("022", 10);
> 22