对数组进行排序(按照每一项中number的大小排序)
es5数组排序的方式:从大到小的排序
this.mythreeawardlist=[{amount:“10”,title:“洗衣机”},{amount:“102”,title:“洗衣机1”},{amount:“56”,title:“洗衣机2”},{amount:“76”,title:“洗衣机3”},{amount:“320”,title:“洗衣机4”},{amount:“77”,title:“洗衣机5”}]
var max;
for(var i=0;i<this.mythreeawardlist.length;i++){
for( var j=i;j<this.mythreeawardlist.length;j++){
if(this.mythreeawardlist[i].amount<this.mythreeawardlist[j].amount){
//如果arr[j]大就把此时的值赋值给最大值变量max
max=this.mythreeawardlist[j];
this.mythreeawardlist[j]=this.mythreeawardlist[i];
this.mythreeawardlist[i]=max;
}
}
}
console.log(“this.mythreeawardlist”, this.mythreeawardlist)
es6数组排序:利用新增的sort方法
从大到小:
this.mythreeawardlist.sort(function (a, b) { return b.amount - a.amount; })
从小到大:
this.mythreeawardlist.sort(function (a, b) { return a.amount - b.amount; })
数组的排序(非全部数字)
最新推荐文章于 2023-05-24 14:41:27 发布