Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
Javascript - Sort multiple objects in array and sort them by specific value

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>
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda