题解 | #表格排序#

表格排序

http://www.nowcoder.com/practice/a23d261947194d5d8e2687aa873d96e4

function sort(type, order) {
    const tbody = document.querySelector('#jsList')
    let idx = 0
    if (type === 'price') {
        idx = 1
    } else if (type === 'sales') {
        idx = 2
    }
    const asc = function (a, b) {
        return a.children[idx].innerHTML - b.children[idx].innerHTML
    }
    const desc = function (a, b) { return b.children[idx].innerHTML - a.children[idx].innerHTML }
    const arrTbody = Array.from(tbody.children)
    sortFun = order === 'asc' ? asc : desc
    arrTbody.sort(sortFun)
    arrTbody.forEach((itm) => {
        tbody.appendChild(itm)
    })
}
全部评论

相关推荐

01-12 17:45
门头沟学院 Java
985废物一枚:就是问问你能不能接受北京的房租,hr也知道公司工资不高,大概率是要贴钱的
找实习记录
点赞 评论 收藏
分享
评论
3
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务