vanilla javascript starter here.
I’m trying to sort some of my elements that I have in a list. After browsing a lot of existing solutions but getting none of them to work I think it’s an issue that I don’t know how to formulate what I’m looking for so maybe someone more experienced can help me out.
So I’m trying to get my list sorted alphabetically and then changed in the htmlcollection.
the items look like this:
<li class="foodListItem" title = "potato">
<div class="img">
<a href="" title="potato">
<img src = "FoodImages/potato.jpg" alt="Potato">
</a>
</div>
<p class="name">
<a href="" title="potato"> Potato</a>
</p>
</li>
<li class="foodListItem" title="brocolli">
<div class="img">
<a href="" title="brocolli">
<img src = "FoodImages/brocolli.jpg" alt="brocolli">
</a>
</div>
<p class="name">
<a href="" title="brocolli"> brocolli</a>
</p>
</li>
And the closest I’ve gotten to replacing it has been this approach:
function sortImages()
{
var foodList= document.getElementsByClassName("foodListItem");
foodArray = Array.from(foodList);
foodArray.sort(function (a, b){
if(a.title < b.title)
{
return -1;
}
if(a.title > b.title){
return 1;
}
return 0;
});
for(var i = 0; i < foodArray.length; i++)
{
foodList[i].replaceWith(foodArray[i]);
}
}
With this approach however I seem to be losing elements whilst replacing them and otherwise I haven’t gotten it to replace correctly using other methods.
Anyone know a better solution?
2
Answers
You can actually benefit from
getElementsByXy()
methods returning live collections. Then you have to find a way to move the items around. A method which I remember isNode.insertBefore()
, which allows kind-of bubble sort (I didn’t fully think through if it’s really a bubble sort):Now as I looked into it, there is
Element.insertAdjacentElement()
which doesn’t need theparentNode
ugliness, and would allowafter
too (not used here):replaceWith()
truncates the live list: a given HTML element instance can exist in a document only once, so if you replacex
withy
, thenx
is actually moved, it disappears from its original location, and thus the HTMLCollection becomes one element shorter. This way the loop fails around the middle (there may be cases when an element replaces itself), and the original list is approximately halved:As it’s a live collection, it has no explicit add operation, but technically you can extend the document itself, though I wouldn’t call that intuitive: check if
i
went pastlength
, and useinsertAdjacentElement()
when it does: