function sortTable()

in assets/android-metrics.js [8:32]


function sortTable(columnIndex, tableId) {
  const table = document.getElementById(tableId);
  const tbody = table.getElementsByTagName('tbody')[0];
  const rows = Array.from(tbody.getElementsByTagName('tr'));

  const isAscending = table.getAttribute('data-sort-direction') === 'asc';
  const direction = isAscending ? 1 : -1;

  rows.sort((a, b) => {
    const cellA = a.getElementsByTagName('td')[columnIndex].innerText.toLowerCase();
    const cellB = b.getElementsByTagName('td')[columnIndex].innerText.toLowerCase();

    if (!isNaN(cellA) && !isNaN(cellB)) {
      return direction * (parseFloat(cellA) - parseFloat(cellB));
    } else {
      return direction * cellA.localeCompare(cellB);
    }
  });

  while (tbody.firstChild) {
    tbody.removeChild(tbody.firstChild);
  }
  rows.forEach(row => tbody.appendChild(row));
  table.setAttribute('data-sort-direction', isAscending ? 'desc' : 'asc');
}