Basic array sort() lesson | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Basic array sort() lesson

Good afternoon! My question is: why the sort() is considering 10 to come first than 5? He is sorting like alphabetic sorting of windows explorer instead of considering the value. How can I fix this? Thank you for your time! https://code.sololearn.com/Wc2TVtKbivt9/?ref=app

7th Aug 2020, 5:10 PM
Bruna
Bruna - avatar
2 Answers
+ 2
By default sort is a lexicographic. To sort numerically you have to pass it a function. .sort(function (a, b) { return a - b; })
7th Aug 2020, 5:15 PM
JME
0
Got it! Thank you very much!
7th Aug 2020, 6:36 PM
Bruna
Bruna - avatar