题解 | #表格排序#

表格排序

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

function sort(type, order) {
    const frag = document.createDocumentFragment();
    const trList = [].slice.call(jsList.children,0);
    const len = trList.length;
    let col;
    switch(type) {
        case 'id':
            col = 0;
            break;
        case 'price':
            col = 1;
            break;
        case 'sales':
            col = 2;
            break;
        default:
            col = 0;
            break;
    }
    const fn = order == 'asc' 
                ? (a,b) => getVal(a,col) - getVal(b,col)
                : (a,b) =>getVal(b,col) - getVal(a,col);
    trList.sort(fn);
    
    for(let i = 0; i<len; i++) {
        frag.append(trList[i]);
    }
    
    jsList.innerHTML = '';
    jsList.append(frag);
}

function getVal(target,col) {
    return target.children[col].innerText * 1;
}
sort('sales','asc');

全部评论

相关推荐

05-29 20:34
门头沟学院 C++
KarlAllen:得做好直接春招的准备。学历差的话,一是面试要求会比学历好的严格不少,二是就算面试通过了也会被排序。总之暑期和秋招对于学历差的就是及其不友好
无实习如何秋招上岸
点赞 评论 收藏
分享
06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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