Ben Mustapha Sabrine Ben Mustapha Sabrine - 4 months ago 10
JSON Question

Sorting an array of JavaScript objects according to date

i have that file json:

var Point = [{

"id": 1,
"name": "A",
"LastUpdate": "2016-07-08",
"position": [36.8479648, 10.2793332]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-07",
"position":[ 36.8791039, 10.2656209]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-09",
"position": [36.9922751, 10.1255164]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-10",
"position": [36.9009882, 10.3009531]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-04",
"position": [37.2732415, 9.8713665]}];


How can i sort the objects by the LastUpdate(it's a date) property in ascending and descending order using only JavaScript?

Answer

Try like this

Point.sort(function(a, b) {
  return (new Date(b.LastUpdate)) - (new Date(a.LastUpdate))
})

DEMO