Home > Mobile >  sorting by first digit
sorting by first digit

Time:01-19

I have array [1, 2, 3, 4, 10, 11, 12, 20, 29, 30, 39, 40, 49, 101, 110, 119, 123]

I want to sort by keeping first digit e.g

1
10
101
11
110
119
12
123
2
20
29
3
30
39
4
40
49

is there a correct name for this sort? tried text and different methods unsure how to do this

CodePudding user response:

.sort() should reorder the items like that by default.

The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

console.log(
  [1, 2, 3, 4, 10, 11, 12, 20, 29, 30, 39, 40, 49, 101, 110, 119, 123].sort()
);

  •  Tags:  
  • Related