I'm trying to create a scoreboard with a username and score that is sorted in numeric order. At the moment the scoreboard is sorted based on the username.
The score list should be sorted based on the score value but I'm not sure how. Do I have to change the data type somehow to be able to sort?
Javascript (data is from firebase)
const scorelist = document.querySelector("#scorelist")
function gotData(data) {
var scores = data.val();
var keys = Object.keys(scores);
console.log(keys)
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
var score = scores[k].score;
const newListItem = document.createElement("ul", score);
newListItem.textContent = k + " - " + score;
scorelist.appendChild(newListItem);
}
console.log()
}
function errData(err) {
console.log('Error!');
console.log(err)
}
HTML code:
<p>
<ul id ="scorelist">
</ul>
</p>
` is not a list item (`- `). You're generating invalid markup right now.
– Andreas Aug 13 '21 at 11:32