题解 | #表格排序#
表格排序
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)
})
}