0

I Have this following multidimensional array that I want to sort by 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 is my 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));
Tushar
  • 85,780
  • 21
  • 159
  • 179

1 Answers1

1

See the 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 to sort

DEMO

Tushar
  • 85,780
  • 21
  • 159
  • 179