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

249
Views
JavaScript: How do I sort times in a nested array?

I'm working with a JSON file that contains the 2021 NFL schedule. I'm able to easily filter out Thursday, Sunday & Monday games. The filtering method returns my Sunday games in an array of objects.

Below is a look at how my Sunday array is set up. This code just shows the first two weeks for the Packers, but there are 18 weeks in the schedule, and I have the same info for each team.

So for the first week, for example, I'll have an array of 14 games. (The 14 teams are the visiting teams.) I need to loop through the games for the desired week and sort the array by the time of the games. A typical Sunday will have games at 12:00 PM, 3:25 PM, and 7:15 PM. There are other starting times throughout the season.

[
    {
      "name": "Green Bay Packers",
      "id": 1,
      "nickname": "Packers",
      "homefield": "Lambeau Field",
      "division": "NFC North",
      "schedule": [
        {
          "week": "1",
          "date": "Sept. 12",
          "weekday": "Sunday",
          "time": "3:25 PM",
          "network": "Fox",
          "opponent": "New Orleans Saints",
          "homeAway": "away",
          "venue": "Mercedes-Benz Superdome"
        },
        {
          "week": "2",
          "date": "Sept. 19",
          "weekday": "Monday",
          "time": "7:15 PM",
          "network": "ESPN",
          "opponent": "Detroit Lions",
          "homeAway": "home",
          "venue": "Lambeau Field"
        },
    etc.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Figured it out. "su" is my array of Sunday games, so I pass that and the week number into my function.

function sortSundayByTime(su, weekNo) {
  su.sort((a, b) => {
    if (a.schedule[weekNo].time < b.schedule[weekNo].time) {
      return -1;
    } else if (a.schedule[weekNo].time > b.schedule[weekNo].time) {
      return 1;
    }
  });
}
about 4 years ago · Juan Pablo Isaza Report
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!