题解 | #表格排序#
表格排序
https://www.nowcoder.com/practice/a23d261947194d5d8e2687aa873d96e4
function sort(type, order) { let jsList = document.querySelector("#jsList"); // console.log(jsList) let obj = { id: 0, price: 1, sales: 2, }; if (order === "asc") { let arr = Array.from(jsList.children).sort((a, b) => { return ( a.children[obj[type]].innerText - b.children[obj[type]].innerText ); }); jsList.innerHTML = ""; arr.forEach((item) => { jsList.appendChild(item); }); } if (order === "desc") { let arr = Array.from(jsList.children).sort((a, b) => { return ( b.children[obj[type]].innerText - a.children[obj[type]].innerText ); }); jsList.innerHTML = ""; arr.forEach((item) => { jsList.appendChild(item); }); } }