2015-06-29 78 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