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}
5
  • 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. Commented Nov 2, 2017 at 7: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 Commented Nov 2, 2017 at 7:47
  • look here: stackoverflow.com/questions/5166842/sort-dates-in-python-array Commented Nov 2, 2017 at 7:48
  • @Bacchus only dates sort i have getting success but i want also with my data dates wise sort. Commented Nov 2, 2017 at 7:49
  • @Bacchus Where did you find a Python reference in the question? O.o Commented Nov 2, 2017 at 7:49

3 Answers 3

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

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

2 Comments

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.
@NinaScholz Took me a while but yes, you're totally right! Thanks for pointing that out.
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

1 Comment

You don't need the getTime() here. Dates are converted to numbers (timestamps) by the - operator.
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.

2 Comments

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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.