Toggle navigation
☰
Home
HTML
CSS
Scripting
Database
<!DOCTYPE html> <title>My Example</title> <style> .sortable-table { width: 100%; border-collapse: collapse; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; } .sortable-table thead th { padding: 10px 15px; background-color: #f2f2f2; border-bottom: 2px solid #ccc; text-align: left; font-weight: 600; } /* Style for sortable headers */ .sortable-table th.sortable-header { cursor: pointer; position: relative; padding-right: 30px; /* Make space for the icon */ } .sortable-table th.sortable-header:hover { background-color: #e9e9e9; } /* Sort icon styling */ .sortable-table .sort-icon { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); opacity: 0.4; transition: opacity 0.2s; } .sortable-table .sortable-header:hover .sort-icon { opacity: 1; } .sortable-table tbody td { padding: 10px 15px; border-bottom: 1px solid #ddd; } .sortable-table .td-number { text-align: right; } </style> <table class="sortable-table" id="mySortableTable"> <thead> <tr> <!-- Add 'sortable-header' class to headers you want to be sortable --> <th scope="col" class="sortable-header">Country</th> <th scope="col" class="sortable-header">Capital City</th> <th scope="col" class="sortable-header td-number">Population (millions)</th> <th scope="col" class="sortable-header td-number">Area (km²)</th> </tr> </thead> <tbody> <tr><td>Japan</td><td>Tokyo</td><td class="td-number">125.7</td><td class="td-number">377975</td></tr> <tr><td>Brazil</td><td>BrasÃlia</td><td class="td-number">212.6</td><td class="td-number">8515767</td></tr> <tr><td>Canada</td><td>Ottawa</td><td class="td-number">38.0</td><td class="td-number">9984670</td></tr> <tr><td>Germany</td><td>Berlin</td><td class="td-number">83.2</td><td class="td-number">357022</td></tr> <tr><td>Australia</td><td>Canberra</td><td class="td-number">25.7</td><td class="td-number">7692024</td></tr> <tr><td>India</td><td>New Delhi</td><td class="td-number">1380.0</td><td class="td-number">3287263</td></tr> </tbody> </table> <script> /** * Makes an HTML table sortable. * * @param {HTMLTableElement} table The table element to make sortable. */ function makeTableSortable(table) { const headers = table.querySelectorAll('.sortable-header'); let currentSort = { column: null, isAscending: true }; headers.forEach((header, headerIndex) => { // Add a sort icon to each header const sortIcon = document.createElement('span'); sortIcon.classList.add('sort-icon'); sortIcon.innerHTML = ' ↕'; header.appendChild(sortIcon); header.addEventListener('click', () => { // Determine sort direction if (currentSort.column === headerIndex) { currentSort.isAscending = !currentSort.isAscending; } else { currentSort.column = headerIndex; currentSort.isAscending = true; } const tableBody = table.querySelector('tbody'); const rows = Array.from(tableBody.querySelectorAll('tr')); // Sort the rows rows.sort((a, b) => { const aValue = a.children[headerIndex].innerText; const bValue = b.children[headerIndex].innerText; // Check if the values are numbers const isNumber = !isNaN(parseFloat(aValue)) && isFinite(aValue); if (isNumber) { return currentSort.isAscending ? parseFloat(aValue) - parseFloat(bValue) : parseFloat(bValue) - parseFloat(aValue); } else { return currentSort.isAscending ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue); } }); // Re-append rows in sorted order rows.forEach(row => tableBody.appendChild(row)); // Update sort icons updateSortIcons(headers, currentSort); }); }); } function updateSortIcons(headers, currentSort) { headers.forEach((header, index) => { const icon = header.querySelector('.sort-icon'); if (index === currentSort.column) { icon.innerHTML = currentSort.isAscending ? ' ↓' : ' ↑'; icon.style.opacity = '1'; } else { icon.innerHTML = ' ↕'; icon.style.opacity = '0.4'; } }); } // Initialize the sortable table const myTable = document.getElementById('mySortableTable'); if (myTable) { makeTableSortable(myTable); } </script>