Estoy tratando de ordenar una matriz por los tiempos más rápidos. Los tiempos provienen de mi base de datos, que da 2 números (dobles) diferentes y un nombre (String). En el siguiente código combino los 2 tiempos para tener un tiempo combinado, que coloco en la matriz junto con el nombre como un objeto.
Mi problema es que no puedo ordenar la matriz por la parte de "tiempo" del objeto que he creado.
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(); });Código HTML:
<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>