0
Why does this return 10,5?
sort(5,10)
7 Answers
+ 3
sajid chowdhury it's a default feature of sort function in JavaScript. Actually it is not sorting number. It's sorting string value means here numbers are considered as String and as you know that when we sort string it sort on character by character so here 1 is less than 5 that's why answer is (10, 5)
If you want to sort in ascending or descending order Just do like this.
[5, 10].sort(function (a, b) {
return a - b
});
[5, 10].sort(function (a, b) {
return b - a
});
+ 1
sajid chowdhury Sort character by character like in your case 10 and 5, 1 is less than 5 so 10 will come first.
+ 1
A J no I meant that I've to change default behaviour of sort method for numbers. But anyways thanks. I got my answer.
0
Camcoder I asked why. I already know the solution. And the solution you gave is useless because I'm taking inputs from the user
0
@so is 10,5 in ascending order?
0
Camcoder if you don't know the answer why are you even replying
0
A J so sort by default can only sort alphabets?