Arrival predictions showing in random order

Whenever I try to parse some JSON data for arrival predicitions via a URL, the timeToStation field always appears in a random order rather than chronologically with the nearest train first. Does anyone know why this is happening?

https://api.tfl.gov.uk/Line/victoria/Arrivals/940GZZLUPCO?direction=inbound

I’ve no idea why this happens, but I use this to correct it:

data.sort(sortSoonest);

function sortSoonest(a, b)
{
	return a.timeToStation - b.timeToStation;
}

I added that many years ago, so it has always done this, at least occasionally.

Perhaps the entries are in timetable order and the buses or trains aren’t all running uniformly late. But that’s just a guess.

How can this be added when the JSON code is generated from a URL?

Looking at the output, it appears to be in Vehicle ID order rather than order of arrival. I have no idea whether this refers to the train itself or to the “running number” it is given each day.

London Vehicle Finder offers the option to support by vehicle ID (which in that case definitely relates to the bus itself) or where they are on the route so I guess they are applying the necessary technique to get whichever is not the default.

To be fair, there isn’t any documentation to suggest that the output should come in any particular order. With APIs, it’s up the the programmer to sort the output for display/storage.

Thanks for that. It’s a shame that Android hasn’t provided decent documentation to impelent this data otherwise I would’ve done it in a jiffy.