/** * Table sorting functionality for the leaderboard * Allows clicking column headers to sort by model name or benchmark scores */ let currentSortColumn = null; let currentSortDirection = 'desc'; /** * Sort the leaderboard table by the specified column index * @param {number} colIndex - The column index to sort by (0 = model name, 1+ = benchmarks) */ function sortTable(colIndex) { const table = document.querySelector('#leaderboardTable'); if (!table) return; const tbody = table.querySelector('tbody'); if (!tbody) return; const rows = Array.from(tbody.querySelectorAll('tr')); // Toggle sort direction if clicking same column if (currentSortColumn === colIndex) { currentSortDirection = currentSortDirection === 'desc' ? 'asc' : 'desc'; } else { currentSortColumn = colIndex; currentSortDirection = 'desc'; } // Sort rows rows.sort((a, b) => { let aVal, bVal; if (colIndex === 0) { // Sort by model name (stored in data-name attribute) aVal = a.dataset.name || ''; bVal = b.dataset.name || ''; return currentSortDirection === 'asc' ? aVal.localeCompare(bVal) : bVal.localeCompare(aVal); } else { // Sort by benchmark score const aCell = a.cells[colIndex]; const bCell = b.cells[colIndex]; // Extract score from cell text content const aText = aCell ? aCell.textContent.trim() : ''; const bText = bCell ? bCell.textContent.trim() : ''; // Parse scores (handle "—" as missing = -1) const aScore = aText === '—' ? -1 : parseFloat(aText); const bScore = bText === '—' ? -1 : parseFloat(bText); // Handle missing scores - put them at the end if (isNaN(aScore) && isNaN(bScore)) return 0; if (isNaN(aScore)) return 1; if (isNaN(bScore)) return -1; // Both are numbers, compare them return currentSortDirection === 'desc' ? bScore - aScore : aScore - bScore; } }); // Re-append rows in sorted order rows.forEach(row => tbody.appendChild(row)); // Update sort indicators updateSortIndicators(colIndex); } /** * Update the sort direction indicators in column headers * @param {number} colIndex - The currently sorted column index */ function updateSortIndicators(colIndex) { const headers = document.querySelectorAll('#leaderboardTable thead th'); headers.forEach((th, index) => { const sortArrow = th.querySelector('.sa'); if (sortArrow) { if (index === colIndex) { // Update arrow for sorted column sortArrow.textContent = currentSortDirection === 'desc' ? '↓' : '↑'; th.classList.add('sorted'); } else { // Reset arrow for other columns sortArrow.textContent = '↕'; th.classList.remove('sorted'); } } }); }