sort()对象数组‘数字、字符串数组排序应用

 

根椐数组中某一对象的某个属性进行排序(数组的对象是数字或对象)

x y 指数组中的对象,y.Reduction x.Reduction了数组对象的某个属性

Runners对象数组应用:

var arr =Runners.sort(function (x, y) {

return (parseFloat(y.Reduction) - parseFloat(x.Reduction)); //把百分比转成数字进行比较 如:12% -----》0.12
});

 

 

跟进:

因为sort()是按照字母表顺序排序,所以不能为数值数组排序。因此,为了正确的对数值排序,必须为sort()方法定义一个比较函数:

numbers = Runners.sort(

function(x,y)

{

return (x - y);

}

);

posted on 2015-04-16 11:48  高达  阅读(676)  评论(0)    收藏  举报

导航