diff --git a/lib/departures.js b/lib/departures.js index f85dd00..fda49e1 100644 --- a/lib/departures.js +++ b/lib/departures.js @@ -45,7 +45,7 @@ const departures = (station, opt) => { dep.direction = shorten(dep.direction) return dep }) - .sort((a, b) => a.when > b.when) + .sort((a, b) => new Date(a.when) - new Date(b.when)) : [] }) } diff --git a/test/index.js b/test/index.js index ded2b24..b6c9dce 100644 --- a/test/index.js +++ b/test/index.js @@ -241,6 +241,21 @@ test('departures at 7-digit station', (t) => { .catch(t.ifError) }) +test('departures should be sorted', (t) => { + hafas.departures('900000230091', { // Portsdam Sternwarte + when, + duration: 120 // see derhuerst/vbb-rest#18 + }) + .then((deps) => { + const sorted = deps.sort((a, b) => { + return new Date(a.when) - new Date(b.when) + }) + t.deepEqual(sorted, deps, 'deps are not sorted by when field') + t.end() + }) + .catch(t.ifError) +}) + test('nearby', (t) => {