Since I am a beginner, I don't know how to start sorting the date column. I have another column which is total price in my table. I managed to sort the price column since it only involves number only.
The codes I provided below is how I successfully sort price column.
//function to sort number
function sortLowest() {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("example");
switching = true;
while (switching) {
switching = false;
rows = table.rows;
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[6];
y = rows[i + 1].getElementsByTagName("TD")[6];
if (parseInt(x.innerHTML.toLowerCase()) > parseInt(y.innerHTML.toLowerCase())) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
Below are the html code
<p id="rating">Filter by Price:
<select id="myList" class='form-control' style="width: 100%">
<option id="stylesheet1" value="low">Lowest-Highest</option>
<option id="stylesheet2" value="high">Highest-Lowest</option>
</select></p>
<script>
document.getElementById("myList").onchange = function() {
var sheet = document.getElementById("myList").value;
if (sheet == "high") {
sortHighest();
} else {
sortLowest();
}
return false
};
</script>
