1

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?

6 Answers6

3

Try like this

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

DEMO

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
2

As your date format is YYYY-MM-DD ( year, followed by month, followed by date ), you can simply compare them as strings using String's localeCompare() method.

Then, you just need to add this to your custom sort function as described below:

var Points = [{

"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]}];

Points.sort(function(a, b){
  return a.LastUpdate.localeCompare( b.LastUpdate );
});

console.log( Points );
Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64
1

As string are in ISO format, they can be sorted by direct string comparison, so no need to convert them in different type of object.

As pointed by Nina in comments, chrome uses different sorting algorithm for arrays larger than 10, so after all the benchmark test, the best method to sort will be

1st in performance (Avg. ms per task 0.011690500000258907)

Point.sort(function(a,b) {
return (a.LastUpdate > b.LastUpdate) ? 1 : ((b.LastUpdate > a.LastUpdate) ?-1:0);
});

Than 2nd in performance (Avg. ms per task 0.029657999999879395)

Point.sort(function(a, b) {
  return a.LastUpdate.localeCompare( b.LastUpdate );
})

3rd in performance (Avg. ms per task 0.03225850000019418)

Point.sort(function(a, b) {
  return (new Date(b.LastUpdate)) - (new Date(a.LastUpdate))
})
Sumit
  • 2,190
  • 23
  • 31
  • sort expect a callback with values smaller than `0`, zero or greater than `0`, according to the sort order. – Nina Scholz Jul 11 '16 at 11:12
  • @Nina my greater than or less than check will give you same result as true and false are equivalent to 0 and 1, you can try the script as well with same numbers – Sumit Jul 11 '16 at 11:14
  • @NinaScholz I am not able to find any problem in my code, can you please describe me where it will fail? – Sumit Jul 11 '16 at 11:37
  • it's just a misuse of sort if no value smaller than 0 is returned. you could make a check, how much itereation it need for the result and how much it needs with proper return value. – Nina Scholz Jul 11 '16 at 11:47
  • the acepted anwer has no good performance, because of the superfluous date juggeling. but you miss the part, where you make the callback faster with a stable return value. – Nina Scholz Jul 11 '16 at 12:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116979/discussion-between-sumit-kumar-and-nina-scholz). – Sumit Jul 11 '16 at 12:08
0

You can use the sort function with a compare function.

Point.sort(function(a, b) {
    var aSplit = a.LastUpdate.split("-"),
        aDate = new Date(aSplit[2], aSplit[1], aSplit[0]),
        bSplit = b.LastUpdate.split("-"),
        bDate = new Date(bSplit [2], bSplit [1], bSplit [0]);

    return aDate - bDate;
});
0

Simple solution using Array.sort(with ES6 "arrow" function) and Date.parse function:

// ascending order
Point.sort((a, b) => Date.parse(a.LastUpdate) - Date.parse(b.LastUpdate));
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

ascending and descending :

ascPoint = Point.sort((a,b) => Date.parse(a.LastUpdate) -Date.parse(b.LastUpdate));
descPoint = ascPoint.reverse();
kevin ternet
  • 4,514
  • 2
  • 19
  • 27