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?

2

6 Answers 6

3

Try like this

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

DEMO

Sign up to request clarification or add additional context in comments.

Comments

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 );

Comments

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))
})

6 Comments

sort expect a callback with values smaller than 0, zero or greater than 0, according to the sort order.
@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
@NinaScholz I am not able to find any problem in my code, can you please describe me where it will fail?
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.
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.
|
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;
});

Comments

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));

Comments

0

ascending and descending :

ascPoint = Point.sort((a,b) => Date.parse(a.LastUpdate) -Date.parse(b.LastUpdate));
descPoint = ascPoint.reverse();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.