开发者

Javascript - sorting arrays containing positive and negative "decimal" numbers

开发者 https://www.devze.com 2023-01-02 05:38 出处:网络
I originally had the following callback passed as a parameter to the javascript array sort() function:

I originally had the following callback passed as a parameter to the javascript array sort() function:

function sortNumber(a,b) {
return a-b;
}

However this doesn开发者_运维知识库't work when my array contains positive and negative decimal numbers (i.e. -107.578, 97.453 etc.) How would I modify this to sort properly?


I don't see any problems with that function. Here's my test code:

var nums = [10, 5, 40, 25, -3412,4212, -107.578, 97.453];

function sortNumber(a,b){
   return a - b;
}

alert( nums.sort(sortNumber) );

Can you show some more of your code? It might be a problem with the array.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号