I am trying to order a JSON object, by using an integer value which is nested in it. the approach I have taken is to order the incoming data as readably as possible. Which I create a new JSON object with:
var newDataFormat = {"route": route, "destination": destination, "countdown": countdown};
busJSON.push(newDataFormat);
Then I create an array to act as the ordering mechanism. Which takes the 'countdown' integer, and then sorts by that number:
for (var i = totalStops - 1; i >= 0; i--) {
countdownArray.push(parseInt(busJSON[i].countdown));
}
countdownArray.sort(function(a,b){return a - b});
Once I have these, I have one last loop, to push the information to the browser:
for (var i = dataMin; i < dataMax; i++) {
if (countdownArray[i] === i) {
items.push("<tr><td>" + busJSON[i].route + "</td><td>" + busJSON[i].destination + "</td><td>" + busJSON[i].countdown + "</td></tr>");
}
}
The problem seems that it doesn't quite push all of the items to the browser, which I have made a demo of here:
http://tfl.applied-espi.com/bus-popup/
Is there a more correct/efficient way I should be doing this?