7 回答
TA贡献1785条经验 获得超4个赞
var drawerList = [
{url:'../../static/images/icon/coin-etc.png',name:'ETC',zhangfu:+122.33,price:228.253,about:3.3248,state:1},
{url:'../../static/images/icon/coin-bat.png',name:'BAT',zhangfu:-12.20,price:68.53,about:3.3248,state:0},
{url:'../../static/images/icon/coin-gram.png',name:'GRA',zhangfu:+121.13,price:68.3223022,about:3.3248,state:1},
{url:'../../static/images/icon/coin-eth.png',name:'ETH',zhangfu:+12.83,price:68.25232,about:3.3248,state:1},
{url:'../../static/images/icon/coin-bat.png',name:'BAT',zhangfu:-122.03,price:12.25322232,about:3.3248,state:0},
{url:'../../static/images/icon/coin-etc.png',name:'ETC',zhangfu:-12.13,price:68.253232,about:3.3248,state:0},
{url:'../../static/images/icon/coin-bat.png',name:'BAT',zhangfu:-9.03,price:68.253232,about:3.3248,state:0},
]
function sortList(a, b) {
return b.zhangfu - a.zhangfu
}
drawerList.sort(sortList)
console.log(drawerList)
TA贡献1788条经验 获得超4个赞
let obj = {
drawerList:[
{url:'../../static/images/icon/coin-etc.png',name:'ETC',zhangfu:+122.33,price:228.253,about:3.3248,state:1},
{url:'../../static/images/icon/coin-bat.png',name:'BAT',zhangfu:-12.20,price:68.53,about:3.3248,state:0},
{url:'../../static/images/icon/coin-gram.png',name:'GRA',zhangfu:+121.13,price:68.3223022,about:3.3248,state:1},
{url:'../../static/images/icon/coin-eth.png',name:'ETH',zhangfu:+12.83,price:68.25232,about:3.3248,state:1},
{url:'../../static/images/icon/coin-bat.png',name:'BAT',zhangfu:-122.03,price:12.25322232,about:3.3248,state:0},
{url:'../../static/images/icon/coin-etc.png',name:'ETC',zhangfu:-12.13,price:68.253232,about:3.3248,state:0},
{url:'../../static/images/icon/coin-bat.png',name:'BAT',zhangfu:-9.03,price:68.253232,about:3.3248,state:0},
]
}
//记得不要污染源数据
let arr = obj.drawerList.concat();
//升序
arr.sort(function(i,j){
return i.zhangfu - j.zhangfu
})
TA贡献1998条经验 获得超6个赞
let data = [
{url:'../../static/images/icon/coin-etc.png',name:'ETC',zhangfu:+122.33,price:228.253,about:3.3248,state:1},
{url:'../../static/images/icon/coin-bat.png',name:'BAT',zhangfu:-12.20,price:68.53,about:3.3248,state:0},
{url:'../../static/images/icon/coin-gram.png',name:'GRA',zhangfu:+121.13,price:68.3223022,about:3.3248,state:1},
{url:'../../static/images/icon/coin-eth.png',name:'ETH',zhangfu:+12.83,price:68.25232,about:3.3248,state:1},
{url:'../../static/images/icon/coin-bat.png',name:'BAT',zhangfu:-122.03,price:12.25322232,about:3.3248,state:0},
{url:'../../static/images/icon/coin-etc.png',name:'ETC',zhangfu:-12.13,price:68.253232,about:3.3248,state:0},
{url:'../../static/images/icon/coin-bat.png',name:'BAT',zhangfu:-9.03,price:68.253232,about:3.3248,state:0}
]
// 使用slice拷贝一下数组然后再排序
let result1 = data.slice().sort((a, b) => a.zhangfu - b.zhangfu) // 从小到大
let result2 = data.slice().sort((a, b) => b.zhangfu - a.zhangfu) // 从大到小
TA贡献1735条经验 获得超5个赞
// 从小到大
drawerList.sort((a, b) => {
return a.zhangfu-b.zhangfu
});
// 从大到小
drawerList.sort((a, b) => {
return b.zhangfu-a.zhangfu
});
TA贡献1773条经验 获得超3个赞
万能的 sort 和给力啊,不要考虑那一堆排序方法了
drawerList = ary.sort((a, b) => {
return a.zhangfu - b.zhangfu
})
添加回答
举报