Reputation: 233
I want to sort these array of objects from earliest to latest in regards to time. (The opposite to what it is now i.e. old messages should appear on top of array). I know we can use Date.parse
to turn the dates into milliseconds and then we can use function sortBy(a, b) {
return a - b;
}
function to sort them.
But I am having trouble putting it all together.
Here is the data:
const messagesArray = [
{id: "92196bd0-b326-4b04-9520-1e8e4814f5e4", message: "ghghhghg", message_time: "2020-03-11T12:50:15.473Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "c5a35080-8f5b-49f7-8216-d6b94e543c44", message: "gghhghgh", message_time: "2020-03-11T12:50:13.103Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "2046cdda-47cd-492f-8534-a58fe5f5091f", message: "dffffffffffffff", message_time: "2020-03-11T12:47:45.598Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "649534f7-34d6-45ae-98b2-ac1189cf9337", message: "ddfffffffff", message_time: "2020-03-11T12:47:44.105Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null}
];
Upvotes: 0
Views: 727
Reputation: 29000
As long as your dates are
You can use normal lexicographical (string) sorting here. It's safe, since ISO 8601 formatted dates always go from most significant to least, so sorting them in alphabetic order will always produce ascending dates. Or reverse alphabetic for descending dates:
const messagesArray = [
{id: "92196bd0-b326-4b04-9520-1e8e4814f5e4", message: "ghghhghg", message_time: "2020-03-11T12:50:15.473Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "c5a35080-8f5b-49f7-8216-d6b94e543c44", message: "gghhghgh", message_time: "2020-03-11T12:50:13.103Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "2046cdda-47cd-492f-8534-a58fe5f5091f", message: "dffffffffffffff", message_time: "2020-03-11T12:47:45.598Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "649534f7-34d6-45ae-98b2-ac1189cf9337", message: "ddfffffffff", message_time: "2020-03-11T12:47:44.105Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null}
];
messagesArray.sort((a, b) => a.message_time.localeCompare(b.message_time));
console.log(messagesArray)
If you are already getting them sorted opposite to what you want, you can simply use Array#reverse
:
const messagesArray = [
{id: "92196bd0-b326-4b04-9520-1e8e4814f5e4", message: "ghghhghg", message_time: "2020-03-11T12:50:15.473Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "c5a35080-8f5b-49f7-8216-d6b94e543c44", message: "gghhghgh", message_time: "2020-03-11T12:50:13.103Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "2046cdda-47cd-492f-8534-a58fe5f5091f", message: "dffffffffffffff", message_time: "2020-03-11T12:47:45.598Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "649534f7-34d6-45ae-98b2-ac1189cf9337", message: "ddfffffffff", message_time: "2020-03-11T12:47:44.105Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null}
];
messagesArray.reverse();
console.log(messagesArray)
Upvotes: 1
Reputation: 584
Since you already have them sorted in the opposite way you want, if you read the array from the end, that would be fine too I think!
Upvotes: -2
Reputation: 41
This should work:
const messagesArray = [
{id: "92196bd0-b326-4b04-9520-1e8e4814f5e4", message: "ghghhghg", message_time: "2020-03-11T12:50:15.473Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "c5a35080-8f5b-49f7-8216-d6b94e543c44", message: "gghhghgh", message_time: "2020-03-11T12:50:13.103Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "2046cdda-47cd-492f-8534-a58fe5f5091f", message: "dffffffffffffff", message_time: "2020-03-11T12:47:45.598Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "649534f7-34d6-45ae-98b2-ac1189cf9337", message: "ddfffffffff", message_time: "2020-03-11T12:47:44.105Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null}
];
var sortedByDate = messagesArray.sort(function(a,b) {
return new Date(a.message_time).getTime() - new Date(b.message_time).getTime()
});
console.log(sortedByDate);
Upvotes: 1
Reputation: 62566
In order to sort the objects by time you first need to convert the strings to time objects (that can be comparable):
const messagesArray = [
{id: "92196bd0-b326-4b04-9520-1e8e4814f5e4", message: "ghghhghg", message_time: "2020-03-11T12:50:15.473Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "c5a35080-8f5b-49f7-8216-d6b94e543c44", message: "gghhghgh", message_time: "2020-03-11T12:50:13.103Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "2046cdda-47cd-492f-8534-a58fe5f5091f", message: "dffffffffffffff", message_time: "2020-03-11T12:47:45.598Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null},
{id: "649534f7-34d6-45ae-98b2-ac1189cf9337", message: "ddfffffffff", message_time: "2020-03-11T12:47:44.105Z", interaction_id: "08350527-770e-42eb-ad3c-33a73250bc20", post_user_ref: null}
];
messagesArray.sort((a, b) => {return new Date(a.message_time) - new Date(b.message_time)});
console.log(messagesArray);
Upvotes: 1