<script type="text/javascript">
    // 填写JavaScript
    function sort(type, order) {
        const table = document.getElementById("jsList");
        const rows = Array.from(table.rows);

        let col;
        switch(type){
            case 'id':
                col = 0;
                break;
            case 'price':
                col = 1;
                break;
            case 'sales':
                col = 2;
                break;
            default:
                col = 0;
                break;
        }

        const comparator = order == 'asc'
            ? (a, b) => getVal(a, col) - getVal(b, col)
            : (a, b) =>getVal(b, col) - getVal(a, col);

        rows.sort(comparator);

        // 更新表格内容
        for(let i=1; i<rows.length; i++) {
            table.appendChild(rows[i]);
        }
    }

    function getVal(target, col) {
        return parseFloat(target.cells[col].innerHTML);
    }
</script>