I have functions which after clicking on the button sorts the elements. On the website, I have several buttons which are responsible for sorting different elements. My function looks like this
const sort = () => {
var list = document.querySelector('.list');
console.log([...list.children]);
[...list.children]
.sort((a,b)=>a.querySelector('.video-count').innerText.replace(/,/g, '') - b.querySelector('.video-count').innerText.replace(/,/g, ''))
.map(node=>list.appendChild(node))
}
document.getElementById("video").addEventListener('click', sort);
And if I want to sort other elements, then in the above function I have to change only this element a.querySelector('.video-count') (add new class)
This is my HTML code
<ul class="list">
<li class="channel-wrraper">
<div class="channel-statistic">
<div class="statistic-wrraper">
<span class="statistic-name">subscribers:</span>
<span class="subscirber-count">${subscriberCount}</span>
</div>
<div class="statistic-wrraper">
<span class="statistic-name">videos:</span>
<span class="video-count">${videoCount}</span>
</div>
<div class="statistic-wrraper">
<span class="statistic-name">views:</span>
<span class="veiw-count">${viewCount}</span>
</div>
</div>
</li>
</ul>
How can I solve this problem?