Sorting an HTML Table
The Data Input
For this challenge, we recieved an array of objects that contained metadata about artists who perform a music genre known as bhangra. Each object contained the three attributes:
- name referring to the name of each artist.
- birthYear referring to the year each artist was born.
- link referring to a youtube link for a video of that artist’s work.
This is the array we were given.
const artists = [
{name: "Ms Scandalous",
birthYear: 1985,
link: "https://www.youtube.com/watch?v=2FPivlfvxu0"
,
}
{name: "Juggy D",
birthYear: 1981,
link: "https://www.youtube.com/watch?v=1jAc_-FVjdI"
,
}
{name: "Sukhbir Singh",
birthYear: 1969,
link: "https://www.youtube.com/watch?v=HiprNF9Jad0"
,
}
{name: "Abrar-ul-Haq",
birthYear: 1989,
link: "https://www.youtube.com/watch?v=-lnnVIP7FEc"
,
}
{name: "Rishi Rich",
birthYear: 1970,
link: "https://www.youtube.com/watch?v=O95-w2gACuA"
} ]
Step 1. Making the Table
First, the students must populate a given table element to hold the data. We chose to accomplish this by systematically accumulating HTML tags as string surrounding metadata from the array and inserting the accumulation into the table element.
function populateTable(array) {
let table = document.getElementById('bhangra'); // this code selects the table
// declare a string to hold the inner HTML code for the table
let contents = "<tbody>";
// add the header row
+= `<tr><th>name</th>`;
contents += `<th>birthyear</th>`;
contents += `<th>link</th></tr>`;
contents
.forEach(function (artist) {
array+= `<tr>`; // open the row
contents += `<td>${artist.name}</td>`;
contents += `<td>${artist.birthYear}</td>`;
contents += `<td><a href= "${artist.link}" target="_blank">${artist.link}</a></td>`;
contents += `</tr>`; // close the row
contents
})
// close out the body of the table
+= "</tbody>";
contents
.innerHTML = contents; // this code populates the table with the contents string
table
;
}
// call the function to populate the table
populateTable(artists);
Step 4. Create Event Listeners
Fourth, we needed to create event listeners for each button so the javascript code to sort the data would run when the buttons are pressed.
The functions referrenced in each event listener are referenced later in the document. This is not problematic because JavaScript uses asynchronous programming.
.addEventListener('click', function () {
nameButton.sort(byName);
artists;
})
.addEventListener('click', function () {
yearButton.sort(byYear);
artists;
})
.addEventListener('click', function () {
randomButton.sort(shuffle);
artists; })
Step 5. Functions for Sorting
Fifth and finally, we needed to create a function for each sort method.
// first a function to sort by name:
function byName(a, b) {
populateTable(artists.sort(function (a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
;
}))
;
}
// second a function to sort by year:
function byYear(a, b) {
populateTable(artists.sort(function (a, b) {
if (a.birthYear < b.birthYear) return -1;
if (a.birthYear > b.birthYear) return 1;
return 0;
;
}))
;
}
// third a function to shuffle the rows:
function shuffle() {
for (let i = artists.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
, artists[j]] = [artists[j], artists[i]];
[artists[i]
}
populateTable(artists);
; }
Some parts of the above code were adapted from suggestions by Github Copilot.