Skip to content

Commit 9cd3a24

Browse files
author
Landon Reed
authored
Merge pull request #42 from catalogueglobal/fix-null-stoptime-in-trip-sorter
Fix null stoptime in timetable trip sorter
2 parents 6ae5163 + 6ff3e36 commit 9cd3a24

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/editor/util/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,22 @@ export function sortAndFilterTrips (
134134
? trips
135135
.filter(t => t.useFrequency === useFrequency) // filter out based on useFrequency
136136
.sort((a, b) => {
137-
if (!a.stopTimes[0].departureTime || !b.stopTimes[0].departureTime) {
137+
// There may be a case where a null value (skipped stop) appears first
138+
// in the list of stopTimes. In this case, we want to compare on the
139+
// first stopTime that exists for each pair of trips.
140+
let aStopTime, bStopTime
141+
let count = 0
142+
while (!aStopTime || !bStopTime) {
143+
aStopTime = a.stopTimes[count]
144+
bStopTime = b.stopTimes[count]
145+
count++
146+
}
147+
if (!aStopTime.departureTime || !bStopTime.departureTime) {
138148
return 0
139-
} else if (a.stopTimes[0].departureTime < b.stopTimes[0].departureTime) {
149+
} else if (aStopTime.departureTime < bStopTime.departureTime) {
140150
return -1
141151
} else if (
142-
a.stopTimes[0].departureTime > b.stopTimes[0].departureTime
152+
aStopTime.departureTime > bStopTime.departureTime
143153
) {
144154
return 1
145155
}

0 commit comments

Comments
 (0)