here I try to sort integer array like
var points = [000, 100, 010, 101, 001, 011, 110, 111];
using points.sort();
but the output was 0,1,100,101,110,111,8,9. I was really confused with that, and 8,9 comes from where.
here I try to sort integer array like
var points = [000, 100, 010, 101, 001, 011, 110, 111];
using points.sort();
but the output was 0,1,100,101,110,111,8,9. I was really confused with that, and 8,9 comes from where.
8 is from 010, 9 is from 011, because they are interpreted as octal numbers
You need to remove the preceding zeros
And then you need to use the comparison function in sort() :
points.sort(function(a,b) { return a-b })
That's because the default sorting is based on string comparison.
From the MDN :
If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in Unicode code point order
To sort numbers, do
points.sort(function(a,b) { return a-b })
EDIT : @Daniel pointed another problem, that is the fact your number literals aren't interpreted as you think they are, you should remove the non significant zeros.