多条件排序

//按照keys从小到大排序,如果遇到相同的则val从大到小排序
var timeList = [{
    keys: 3,
    val: "12点"
}, {
    keys: 2,
    val: "24(0)"
}, {
    keys: 2,
    val: "6点"
}, {
    keys: 2,
    val: "18点"
}, {
    keys: 3,
    val: "18点"
}];
timeList.forEach(item => {
    item['val'] = parseInt(item['val'])
})

const paixu = function(field1, field2) {
    return (x, y) => {
        if (x[field1] != y[field1]) {
            return x[field1] - y[field1]
        } else {
            return y[field2] - x[field2]
        }
    }
}
timeList.sort(paixu('keys', 'val'))
console.log(timeList)

发表评论

电子邮件地址不会被公开。 必填项已用*标注