How to sort my objects list by date which is returned from server by a ajax call
I send an Ajax call to server, then server returns me the xml data which contain a list of objects. Each object in the list contain a "date" attribute.
In the success
function of the Ajax call, I would like to populate each object in a row of a html table, but before this, I would like to sort the objects by date ascending order based on the "date" attribute of each object. I am wondering what is the efficient way to do this?
$.ajax({
type : "GET",
url : MY_URL_1,
dataType : "xml",
success : function(xml) {
$(xml).find("DOCUMENT").each(function() {
var eachXMLdata = $(this);
var date = eachXMLdata.children("DATE").text();
// I can check each object's date here
console.log('date:'+date);
/*** How to sort the object by date??****/
//I will show each object in a开发者_C百科 row of a html table here
// ...
});
}
});
As you saw above, I used the .each()
function in Ajax success()
function to loop through each object and will show each object in a row of a html table.
Well , If you do have to do it on client side , construct Date objects with the text , and then you can the getTime() function to get the milliseconds since epoch , which should be easily comaparable .
Refer below for the Date object API http://www.w3schools.com/jsref/jsref_obj_date.asp
精彩评论