1

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}
Lakshmikant Deshpande
  • 826
  • 1
  • 12
  • 30
Edit
  • 385
  • 4
  • 24
  • Post what you've written so far, along with a specific question about what you're having trouble with. Remember, SO is not a code writing service. – STF Nov 02 '17 at 07:46
  • @STF yes i know this is not a code writing service i have try with specific on dates array sort but that time my data is not sort with the dates that why i am put my post here – Edit Nov 02 '17 at 07:47
  • look here: https://stackoverflow.com/questions/5166842/sort-dates-in-python-array – Bacchus Nov 02 '17 at 07:48
  • @Bacchus only dates sort i have getting success but i want also with my data dates wise sort. – Edit Nov 02 '17 at 07:49
  • @Bacchus Where did you find a Python reference in the question? O.o – Andreas Nov 02 '17 at 07:49

3 Answers3

3

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

Werner
  • 2,126
  • 2
  • 22
  • 34
  • 2
    please do not use the result of a single compairing, because you do not respect the negative value. sort is expecting three values, according to the position of two elements. you sorting is only working if all values are different. – Nina Scholz Nov 02 '17 at 08:14
  • @NinaScholz Took me a while but yes, you're totally right! Thanks for pointing that out. – Werner Nov 02 '17 at 08:36
1

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

Ezzat
  • 931
  • 6
  • 13
0

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.

Allan
  • 12,117
  • 3
  • 27
  • 51
  • 2
    dates in _id property are not in valid iso format, so new Date(a._id) will not work for them. You can either use something like `new Date(year, month, date)` or use libraries like moment.js which is quite convenient – paulitto Nov 02 '17 at 07:59
  • Thank you for your comment! I have edited the method and reorder the date string in YYYY-MM-DD. This format is accept as a valid string to create a Date object. – Allan Nov 02 '17 at 08:10