0

Can anybody explain how in this case sort works?

var arrayNumb = [2, 8, 15, 16, 23, 42];
arrayNumb.sort();
console.log(arrayNumb); // [ 15, 16, 2, 23, 42, 8 ]
Nurlan Mirzayev
  • 1,120
  • 1
  • 7
  • 15
  • 5
    it's treating the values as strings. – Nina Scholz Oct 21 '17 at 19:13
  • 1
    Don't you see it's a lexical sort? eg. 15 comes after 1, but before 2 because the first char in 15 is lower than the first char in 2.. Most OS-es sort files like this even when the files are numeric names. – Sylwester Oct 21 '17 at 22:40

1 Answers1

4

See the MDN documentation:

compareFunction Optional
Specifies a function that defines the sort order. If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335