Changed around line 1
+ document.addEventListener('DOMContentLoaded', function() {
+ const tables = document.querySelectorAll('table');
+
+ tables.forEach(table => {
+ const headers = table.querySelectorAll('th');
+ const rows = table.querySelectorAll('tr');
+
+ // Add sorting functionality
+ headers.forEach((header, index) => {
+ header.addEventListener('click', () => {
+ const sortDirection = header.classList.contains('asc') ? -1 : 1;
+ const rowsArray = Array.from(rows).slice(1);
+
+ rowsArray.sort((a, b) => {
+ const aCol = a.querySelectorAll('td')[index].textContent;
+ const bCol = b.querySelectorAll('td')[index].textContent;
+
+ return aCol.localeCompare(bCol) * sortDirection;
+ });
+
+ headers.forEach(h => h.classList.remove('asc', 'desc'));
+ header.classList.toggle('asc', sortDirection === 1);
+ header.classList.toggle('desc', sortDirection === -1);
+
+ const tbody = table.querySelector('tbody');
+ rowsArray.forEach(row => tbody.appendChild(row));
+ });
+ });
+ });
+ });