I am trying to render a high score ordered list using an JavaScript object that looks something like this:
{user1: 5, user2: 10}
Using the following code I can generate each list item individual:
Object.entries(data).forEach(([key, value]) => {
const li = document.createElement('li');
li.innerHTML = `${key}: ${value}`;
scoreList.append(li);
});
Now I am looking for a way to sort each list item based on the object value. I know we have the sort function that works on arrays and using the following code almost works:
const keysSorted = Object.entries(data).sort((a, b) => {
return data[a] - data[b];
});
However it does not keep the object values, it just returns a sorted array with each key. My wanted result looks like this:
<ol>
<li>user2: 10</li>
<li>user1: 5</li>
</ol>