Edit
Edit

Reputation: 393

How to sort an array on dates with data using JavaScript?

I have one array which has dates and some other data. I want to sort this array by using the dates. How should I do that? I have no idea. Please help me out. .

This is my array =>

0:{_id: "01-11-2017", CommentCount: 221, Likecount: 141, Followcount: 0, Unfollowcount: 116}
1:{_id: "27-10-2017", CommentCount: 235, Likecount: 495, Followcount: 802, Unfollowcount: 98}
2:{_id: "28-10-2017", CommentCount: 232, Likecount: 52, Followcount: 0, Unfollowcount: 799}
3:{_id: "29-10-2017", CommentCount: 236, Likecount: 52, Followcount: 0, Unfollowcount: 611}
4:{_id: "30-10-2017", CommentCount: 233, Likecount: 58, Followcount: 0,  Unfollowcount: 0}
5:{_id: "31-10-2017", CommentCount: 240, Likecount: 175, Followcount: 0, Unfollowcount: 0}

Expected output =>

0:{_id: "27-10-2017", CommentCount: 235, Likecount: 495, Followcount: 802, Unfollowcount: 98}
1:{_id: "28-10-2017", CommentCount: 232, Likecount: 52, Followcount: 0, Unfollowcount: 799}
2:{_id: "29-10-2017", CommentCount: 236, Likecount: 52, Followcount: 0, Unfollowcount: 611}
3:{_id: "30-10-2017", CommentCount: 233, Likecount: 58, Followcount: 0, Unfollowcount: 0}
4:{_id: "31-10-2017", CommentCount: 240, Likecount: 175, Followcount: 0, Unfollowcount: 0}
5:{_id: "01-11-2017", CommentCount: 221, Likecount: 141, Followcount: 0, Unfollowcount: 116}

Upvotes: 1

Views: 100

Answers (3)

Werner
Werner

Reputation: 2154

First you need to parse the date-string to a real Date object. After then you can sort it like you want:

const inputs = [
  {_id: "01-11-2017", CommentCount: 221, Likecount: 141, Followcount: 0, Unfollowcount: 116},
  {_id: "27-10-2017", CommentCount: 235, Likecount: 495, Followcount: 802, Unfollowcount: 98},
  {_id: "28-10-2017", CommentCount: 232, Likecount: 52, Followcount: 0, Unfollowcount: 799},
  {_id: "29-10-2017", CommentCount: 236, Likecount: 52, Followcount: 0, Unfollowcount: 611},
  {_id: "30-10-2017", CommentCount: 233, Likecount: 58, Followcount: 0,  Unfollowcount: 0},
  {_id: "31-10-2017", CommentCount: 240, Likecount: 175, Followcount: 0, Unfollowcount: 0}
];

const results = inputs.sort((a, b) => getDate(a._id) - getDate(b._id));

function getDate(s) {
  const parts = s.split('-');
  return new Date(parts.pop(), parts.pop() - 1, parts.pop());
}

console.log(results);

Be aware that this solution is for modern browsers only due the Arrow function.

For more references see Arrow functions and maybe Array pop

Upvotes: 3

Allan
Allan

Reputation: 12438

You can use something like that:

my_array.sort(function(a,b) { 
    return new Date(a._id.split("-").inverse().join("-")).getTime() - new Date(b._id.split("-").inverse().join("-")).getTime() 
});

sort function behavior:

Optional. A function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments, like: function(a, b){return a-b} When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.

Upvotes: 0

Ezzat
Ezzat

Reputation: 911

Your Date format is not supported by JavaScript so you will have to reverse it before sending it to Date constructor before comparing

console.log(arr.sort(function(a,b) {
    return new Date(a._id.split('-').reverse().join('-')).getTime() - new Date(b._id.split('-').reverse().join('-')).getTime()
}))

This will solve the problem

Upvotes: 1

Related Questions