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.
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;
}
});
}