File size: 2,874 Bytes
fa808e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * 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');
      }
    }
  });
}