为了账号安全,请及时绑定邮箱和手机立即绑定

我有这样一个数组对象,我需要做排序操作

我有这样一个数组对象,我需要做排序操作

拉丁的传说 2019-03-14 14:15:30
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},      ],我想分别在click事件中,根据zhangfu字段做从大到小和从小到大对数组做排序请问,这个js方法应该怎么写? 我尝试了几种写法,好像都不太对
查看完整描述

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)


查看完整回答
反对 回复 2019-03-30
?
尚方宝剑之说

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

})


查看完整回答
反对 回复 2019-03-30
?
米琪卡哇伊

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) // 从大到小


查看完整回答
反对 回复 2019-03-30
?
青春有我

TA贡献1784条经验 获得超8个赞

drawerList.sort((a, b) => a.zhangfu - b.zhangfu)


查看完整回答
反对 回复 2019-03-30
?
慕后森

TA贡献1802条经验 获得超5个赞

drawerList.sort((a,b)=>{return a.zhangfu-b.zhangfu;})


查看完整回答
反对 回复 2019-03-30
?
喵喔喔

TA贡献1735条经验 获得超5个赞

// 从小到大

drawerList.sort((a, b) => {

    return a.zhangfu-b.zhangfu

});


// 从大到小

drawerList.sort((a, b) => {

    return b.zhangfu-a.zhangfu

});


查看完整回答
反对 回复 2019-03-30
?
慕容3067478

TA贡献1773条经验 获得超3个赞

万能的 sort 和给力啊,不要考虑那一堆排序方法了


drawerList = ary.sort((a, b) => {

   return a.zhangfu - b.zhangfu

})


查看完整回答
反对 回复 2019-03-30
  • 7 回答
  • 0 关注
  • 403 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信