0

I am having some trouble sorting a nested object by its timestamp. I was hoping for some help...

This is what the object looks like and what I have so far...

useEffect(() => {
if (realtime.length) {
  let unorderedmessage = realtime.concat(messages);
  const orderedMessages = unorderedmessage
    .slice()
    .sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
  setMessages(orderedMessages);
}
}, [realtime]);


[
Object {
  "_id": "a16edcb7-17e1-46ea-b8ce-5ec312d5eb6c",
  "createdAt": 2020-04-23T01:51:48.000Z,
  "index": 1,
  "key": "cid-1710824786",
  "receiver": Array [
    "OUloNoRKdIeyhNFcAAB6VLrZ5yH2",
  ],
  "text": "2–> Olivia to Mike",
  "user": Object {
    "_id": "lTONqS9O00PhkxwHD2EYs05EMwu2",
    "avatar": "123",
  },
},
Object {
  "_id": "5ef28d42-6f7c-45eb-a5e1-59ca700f56b7",
  "createdAt": 2020-04-23T02:01:52.000Z,
  "index": 1,
  "key": "cid-1710824786",
  "receiver": Array [
    "OUloNoRKdIeyhNFcAAB6VLrZ5yH2",
  ],
  "text": "4–> Olivia to mike",
  "user": Object {
    "_id": "lTONqS9O00PhkxwHD2EYs05EMwu2",
    "avatar": "123",
  },
},
Object {
  "_id": "1e07873f-f010-4e9d-be17-9bcb7793695b",
  "createdAt": 2020-04-23T02:02:06.000Z,
  "index": 1,
  "key": "cid-1710824786",
  "receiver": Array [
    "lTONqS9O00PhkxwHD2EYs05EMwu2",
  ],
  "text": "5–> mike to Olivia",
  "user": Object {
    "_id": "OUloNoRKdIeyhNFcAAB6VLrZ5yH2",
    "avatar": "123",
  },
},
Object {
  "_id": "5fecafc3-c608-4156-b88c-f6c57e8e9977",
  "createdAt": 2020-04-23T02:01:20.000Z,
  "index": 1,
  "key": "cid-1710824786",
  "receiver": Array [
    "OUloNoRKdIeyhNFcAAB6VLrZ5yH2",
  ],
  "text": "3–> Olivia to Mike",
  "user": Object {
    "_id": "lTONqS9O00PhkxwHD2EYs05EMwu2",
    "avatar": "123",
  },
},
Object {
  "_id": "fae07391-9968-432a-8a39-0c1be0d7e9ac",
  "createdAt": 2020-04-23T01:51:41.000Z,
  "index": 0,
  "key": "cid-1710824786",
  "receiver": Array [
    "lTONqS9O00PhkxwHD2EYs05EMwu2",
  ],
  "text": "1–> mike to Olivia",
  "user": Object {
    "_id": "OUloNoRKdIeyhNFcAAB6VLrZ5yH2",
    "avatar": "123",
  },
},
],

The messages are still coming in out of order so I am sure I am writing the sort function incorrectly. I am going through these tutorials and docs and it seems to be correct.... https://www.geeksforgeeks.org/sort-an-object-array-by-date-in-javascript/ https://flaviocopes.com/how-to-sort-array-by-date-javascript/ How to sort an array by a date property

I have also tried explicitly writing out the function like:

  let unorderedmessage = realtime.concat(messages);
  unorderedmessage.sort(function(a, b) {
    return new Date(b.createdAt) - new Date(a.createdAt);
  });
  setMessages(unorderedmessage);

but still no luck. Can anyone see what I am doing wrong?

Olivia
  • 1,843
  • 3
  • 27
  • 48

1 Answers1

0

Works like a charm for me: https://playcode.io/583725/ Cannot be the issue in your Object variable definition? You don't need to specify Object, this is enough:

const myNewObject = {firstKey: 'firstValue', secondKeys: 'secondValue'}

But if you do, you have to create it with new keyword:

const myNewObject = new Object {firstKey: 'firstValue', secondKeys: 'secondValue'}

Fide
  • 1,127
  • 8
  • 7