let enum_type = {
views: 1,
subscribers: 2,
favorites: 3,
}
const gridElement = document.querySelector("body > div > section > div > div.grid.grid-cols-1");
const sortTextSelector = `.card .my-2 .flex.justify-center:nth-child(1) h5:nth-child(${enum_type.subscribers})`;
function reorderCards(gridElement, sortTextSelector) {
// 1. Get all cards as an array
const cards = Array.from(gridElement.children);
// 2. Extract sort text for each card
const cardsWithText = cards.map(card => {
let sortTextElement = card.querySelector(sortTextSelector);
sortTextElement = sortTextElement.textContent.trim().replaceAll(",","")
return {
card: card,
text: sortTextElement ? sortTextElement : "",
};
});
// 3. Sort the cards based on the extracted text (assuming numeric sort)
cardsWithText.sort((a, b) => {
const numberA = parseFloat(a.text);
const numberB = parseFloat(b.text);
return numberB - numberA;
});
// 4. Update the grid element with the reordered cards
gridElement.innerHTML = "";
cardsWithText.forEach(cardObj => gridElement.appendChild(cardObj.card));
}
reorderCards(gridElement, sortTextSelector);