The SORT function in javascripts can not return the correct answer in some situations.
I use http://jsconsole.com to test some examples:
[2, 10].sort()[2, 10]
[3, 10].sort()[10, 3]
[9, 10].sort()[10, 9]
[10, 11].sort()[10, 11]
I tested it on windows xp, windows 2008 with firefox and chrome.
I think you are confusing Java with JavaScript, but they are completely different programming languages. I'm almost positive the code is JavaScript.
The default sort functionality for JavaScript sorts by string value, not integer value. You must do the latter manually, but it's pretty simple:
[3,10].sort(function (a, b) { return a - b; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With