2015-06-29 48 views
0

我有以下多维数组,我想按loc_time排序。JavaScript按日期排序多维Json数组

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

其中'result'变量定义?我想你想把排序结果赋给一个'result'变量。 –

回答

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 

DEMO