Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sorting by first digit

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

.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()
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading