乐趣区

关于javascript:es6-进行数组数组对象-排序降序升序

es6 进行数组,数组对象 排序,降序升序
升序

let data = [

            {value: 335, name: '家电'},
            {value: 310, name: '服装'},
            {value: 274, name: '食品'},
            {value: 235, name: '数码'},
            {value: 400, name: '家纺'}
        ]

data.sort(function (a, b) {return a.value – b.value;})

降序

let data = [

            {value: 335, name: '家电'},
            {value: 310, name: '服装'},
            {value: 274, name: '食品'},
            {value: 235, name: '数码'},
            {value: 400, name: '家纺'}
        ]

data.sort(function (a, b) {return b.value – a.value;})

退出移动版