[Solved] Sort row in array as easily as possible (Javascript)


You can use this function:

function sortCol(desc) {
  const tbl = document.querySelector("table>tbody");
  const rows = Array.from(tbl.rows).slice(1).sort((a, b) =>
    a.querySelector("img").src.localeCompare(b.querySelector("img").src)
  );
  if (desc) rows.reverse();
  rows.forEach(row => tbl.appendChild(row));
}

Call it with argument true when you want to have a descending sort order. If you have more than one table on your page, you need to qualify the selector at the start of the function to target the right table.

solved Sort row in array as easily as possible (Javascript)