JavaScript Sort by date multidimensional Json array -
this question has answer here:
i have following multidimensional array want sort loc_time.
var json = [{     "id": 5,     "lieu_depart": "la rochelle",     "lieu_arrivee": "lyon",     "condlocalisation": [{         "id": 1,         "longitude": "-3.120830",         "latitude": "52.801452",         "adresse": "paris",         "loc_time": "25 jun 2012, 8:00 am"     }, {         "id": 3,         "longitude": "2.130122",         "latitude": "48.801408",         "adresse": "versailles",         "loc_time": "15 may 2013, 6:40 pm"     }, {         "id": 5,         "longitude": "-4.120854",         "latitude": "53.80140",         "adresse": "marseille",         "loc_time": "29 jun 2012, 5:47 pm"     }] }]   this javascript code :
function comp(a, b) {     return new date(a.condlocalisation.loc_time).gettime() - new date(b.condlocalisation.loc_time).gettime(); }  result.sort(comp);  $('body').append(json.stringify(result));      
see changes below:
function comp(a, b) {     return new date(a.loc_time).gettime() - new date(b.loc_time).gettime(); }  result[0].condlocalisation.sort(comp); //    ^^^^^^^^^^^^^^^^^^^^  array sort        
Comments
Post a Comment