I'm trying to sort an array by the fastest times. The times comes from my database, which gives 2 different (double) numbers and a (String) name. In the below code I then combine the 2 times to have a combined time, which I put into the array along with the name as an object.
My problem is, I am unable to sort the array by the "time" part of the object I've made?
const divContainer = document.getElementById("container");
const url = 'http://localhost:8080/api/tourdefrance';
async function loadSortByTime(){
const teamList = await fetch(url).then(response => response.json());
const teamListLength = teamList.length;
let timeArray = [];
function time_convert(num){
const hours = Math.floor(num / 60);
const minutes = Math.round(num % 60);
return hours + "t " + minutes + "'";
}
for (let i = 0; i < teamListLength; i++){
const times = teamList[i];
const combinedTime = (times.bjergtid + times.spurttid);
const biker = [
{
name: times.name,
time: time_convert(combinedTime)}
];
console.log(timeArray);
timeArray.push(biker);
console.log(timeArray);
}
timeArray.sort((a,b) => (a.time > b.time) ? 1 : -1);
}
document.addEventListener('DOMContentLoaded', () => {
loadSortByTime();
});
HTML code:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sort By Time</title>
<script src="../js/sortByTime.js" defer></script>
</head>
<body>
<div id="container"></div>
</body>
</html>