我有以下多维数组,我想按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"
}]
}]
这是我的javascript代码:
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));
答案 0 :(得分:0)
请参阅以下更改:
function comp(a, b) {
return new Date(a.loc_time).getTime() - new Date(b.loc_time).getTime();
}
result[0].condlocalisation.sort(comp);
// ^^^^^^^^^^^^^^^^^^^^ Array to sort