Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!