I want to arrange my data.
If the score is 30 the time will be in ascending order. Else I need to arrange the score in descending order. The final Object will be like this:
[{
"Name": "Mac",
"Score": "30",
"Time": "104"
}, {
"Name": "Tomas",
"Score": "30",
"Time": "105"
}, {
"Name": "Alex",
"Score": "28",
"Time": "120"
}, {
"Name": "Ank",
"Score": "26",
"Time": "110"
}]
const text = [{
"Name": "Tomas",
"Score": "30",
"Time": "105"
}, {
"Name": "Ank",
"Score": "26",
"Time": "110"
}, {
"Name": "Alex",
"Score": "28",
"Time": "120"
}, {
"Name": "Mac",
"Score": "30",
"Time": "104"
}]
text.sort(function(a, b) {
return b.Score - a.Score
});
displayCars();
function displayCars() {
document.getElementById("demo").innerHTML =
text[0].Time + " - " + text[0].Name + "<br>" +
text[1].Time + " - " + text[1].Name + "<br>" +
text[2].Time + " - " + text[2].Name + "<br>" +
text[3].Time + " - " + text[3].Name;
}
<p id="demo"></p>
I have tried this code. It sort the data but not as I expected.
');` – mplungjan Mar 29 '23 at 12:39