I have a string array that stores in LocalStorage. Every element of this array is a string "Name:Score". I need to sort this array by score to build a top. How can i do it more effectively? Maybe i should store player's statistics not like an array for more easy sorting.
My code so far:
function saveResult() {
let userName = $("#input-user-name").val();
let userStat = userName + ":" + playerScore;
results = localStorage.getItem("gameResults");
if (results === null) {
localStorage.setItem("gameResults", JSON.stringify([userStat]));
} else {
results = JSON.parse(localStorage.getItem("gameResults"));
results.push(userStat); //sort
localStorage.setItem("gameResults", JSON.stringify(results));
}
updateStatTable();
}