Sorting an HTML Collection with Javascript

Image for post
Image for post

While working on a project, involving a Rails API and a Javascript front-end, I had a database of around 100 items being rendered on the front-end. The data was all being posted to the front-end just fine and everything was working. However, the data was all being posted to the front-end based on the database ID of the seed data, this was fine…but it really bugged me that it was just seemingly random on the page.

So even though it had no effect on what I wanted my project to do, I decided that I wanted to sort it! Now this may not be the best or most efficient solution to my sort problem, but it’s the one that I got to work for me and I was pretty happy with it.

Now in my project the data that I wanted to sort was being rendered in a container class and being created as a <div> card, so the first step was to just create my sort function and grab the cards:

Easy enough, define my function, declared a variable for my container, and they declared another value for my <div> cards. Next, after some research I found that there really wasn’t a way to directly sort the cards as they were, so the solution was to convert my collected HTML elements into an array using the Array.prototype.slice() method:

Now with the Array.prototype.slice() method essentially allows you to take elements from one array and add them to another array, in my case, it can , rather conveniently, take array-like objects and covert them into an actual array. Hurray!

Now that I had all of <div> cards inside of an array, I can use array methods on them! So this means that I can now use .sort to help me arrange all of those pesky <div> cards:

Basically, just going through all of the items in my array comparing the textContent of each item and sorting them in alphabetical order.

So now that I had my array in alphabetical order, all I have to do yet is put my <div> cards back onto my front-end. For the next step, I just cleared out the container holding all my cards, using the variable that I had assigned earlier, by assigning its innerHTML as an empty string: container.innerHTML = "";. That way I had a clean slate to put all of my freshly arranged cards.

The final step was to go back through my array, take each element, append it back into my container, I accomplished this with a simple For loop to iterate over my array:

That’s it! I now had all of elements in a nice alphabetical order, or so I thought. The trouble now became where I ran my nice sort function, since in my program I was using a fetch to gather my data from the database, trying to run sort as part of the fetch would cause the sort function to run before the promise was resolved.

At this point I had spent a little more time than I had expected to, just to satisfy my own idiosyncrasies. So I resolved to tie my function into a button, not exactly what I had set out to do, but at least now I could sort my data at the press of a button!

And for you, just to tie everything together here is all of my code together:

Image for post
Image for post

I hope that this helps, and thank you for reading.

Computer Support Tech turned Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store