Frank Lucas
Frank Lucas

Reputation: 611

sorting javascript array on date

hello friends I have a javascript array and I want to sort it on the date from most recent to old. The sort function isn't working I am not sure what I am doing wrong... Anyone can help me? Many thanks in advance!

[Object, Object, Object, Object, Object]
 0 : Object
 favoriteTimestamp : Object
  date : "2016-09-30 10:45:13.000000"
  timezone : "Europe/Brussels"
  timezone_type : 3

Here is my sort function:

console.log(results);

let sortedResults = results.sort(function(a, b) {
      a.favoriteTimestamp.date - b.favoriteTimestamp.date;
 });

console.log(sortedResults);

Both logs give same output so the sort isn't working :'(

Thanks for any help :)

Upvotes: 0

Views: 52

Answers (1)

Anton Chukanov
Anton Chukanov

Reputation: 655

Sort has to return something.

results.sort(function(a, b) {
      return a.favoriteTimestamp.date - b.favoriteTimestamp.date;
});

In your case will be returned NaN, cuz you're trying to substract string from string.

Upvotes: 1

Related Questions