I would like to organize the data in ascending order based on the DATE after making this ajax call
function parseXML(xml){
var weatherArray = [];
var weatherElement = xml.getElementsByTagName("forecast")[0];
weatherArray.queryTime = weatherElement.getAttribute("queryTime");
weatherArray.queryLocation = weatherElement.getAttribute("queryLocation");
weatherArray.weatherList = [];
function sortDates(a,b){
return parseInt(a.getElementsByTagName("date")[0].childNodes[0].nodeValue) - parseInt(b.getElementsByTagName("date")[0].childNodes[0].nodeValue);
}
var weatherElements = weatherElement.getElementsByTagName("weather");
weatherElements=weatherElements.sort(sortDates);
for(var i=0; i< weatherElements.length; i++){
var weather = {};
weather.year = Number(weatherElements[i].getElementsByTagName("year")[0].childNodes[0].nodeValue);
weather.month = Number(weatherElements[i].getElementsByTagName("month")[0].childNodes[0].nodeValue);
weather.date = Number(weatherElements[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
weather.dayOfWeek = weatherElements[i].getElementsByTagName("dayOfWeek")[0].childNodes[0].nodeValue;
weather.overall = weatherElements[i].getElementsByTagName("overall")[0].childNodes[0].nodeValue;
weather.overallCode = weatherElements[i].getElementsByTagName("overallCode")[0].childNodes[0].nodeValue;
weather.highest = Number(weatherElements[i].getElementsByTagName("highest")[0].childNodes[0].nodeValue);
weather.lowest = Number(weatherElements[i].getElementsByTagName("lowest")[0].childNodes[0].nodeValue);
weatherArray.weatherList.push(weather);
}
return weatherArray;
}
Once displayed in the browser, I want the data to be sorted starting from the record with the DATE 26 followed by 27, then 28... There are more repeated data in the XML that I did not show here.
What could be causing my issue?