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');