Skip to content

Instantly share code, notes, and snippets.

@timiwahalahti
Last active September 21, 2024 07:43
Show Gist options
  • Save timiwahalahti/e294fd932642322efc995cf53d45c3c3 to your computer and use it in GitHub Desktop.
Save timiwahalahti/e294fd932642322efc995cf53d45c3c3 to your computer and use it in GitHub Desktop.
finna reservation sort
javascript:(function() {
function parseDate(dateStr) {
const [day, month, year] = dateStr.trim().split('.').map(Number);
return new Date(year, month - 1, day);
}
function getElementTextContent(element, selector) {
const el = element.querySelector(selector);
return el ? el.textContent.trim() : '';
}
function getQueuePosition(row) {
const text = getElementTextContent(row, 'div.holds-status-information p');
const match = text.match(/Sijainti jonossa:\s*(\d+)/);
return match ? parseInt(match[1], 10) : Infinity;
}
function getFreezeDate(row) {
const strongElements = row.querySelectorAll('div.holds-status-information strong');
for (const strong of strongElements) {
const match = strong.textContent.match(/Jäädytetty.*?(\d{1,2}\.\d{1,2}\.\d{4})/);
if (match) {
return parseDate(match[1]);
}
}
return new Date(9999, 11, 31); // Default if no date is found
}
function isFrozen(row) {
const text = getElementTextContent(row, 'div.holds-status-information');
return text.includes('Jäädytetty');
}
const table = document.querySelector('.myresearch-table tbody');
if (!table) {
alert('Table tbody not found');
} else {
const rows = Array.from(table.querySelectorAll('tr'));
if (rows.length === 0) {
alert('No rows found in the table');
} else {
const frozenRows = rows.filter(isFrozen);
const nonFrozenRows = rows.filter(row => !isFrozen(row));
// Sort non-frozen rows by queue position
nonFrozenRows.sort((a, b) => getQueuePosition(a) - getQueuePosition(b));
// Sort frozen rows by freeze date
frozenRows.sort((a, b) => getFreezeDate(a) - getFreezeDate(b));
// Combine non-frozen and frozen rows
const sortedRows = [...nonFrozenRows, ...frozenRows];
// Append sorted rows back to the table
sortedRows.forEach(row => table.appendChild(row));
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment