1

I have a message JSON object where for each message there is a timeStamp of when it was submitted. I would like to sort that object based on the timeStamp.

For example:

"Messages": {
  "message1": {
    "msg"       : "I'm trying to make this work",
    "timeStamp" : "2018-02-15T06:24:44.12+00:00"
  },
  "message2": {
    "msg"       : "I really need your help SO!",
    "timeStamp" : "2018-03-01T13:57:27+00:00"  
  },
  "message3": {
    "msg"       : "Please assist me dude!",
    "timeStamp" : "2018-03-01T11:57:27+00:00"
  }
}

The timeStamp generated is from momentjs using moment().format();.

The issue is, I'm not really sure how to filter an object based on that timestamp format.

I currently have no working example and cant think of a better way.

Why do I need this?

I'm using this object to show messages between two people, but the messages need to be in order based on time.

Can't you just change the format and make it simple?

No because, currently my whole application is based on that format, and changing it will cause more issues.

Update

I've forgotten mention that the format of the output must be an object after sorting (where most recent is first item)

Doe
  • 193
  • 4
  • 14
  • 1
    why is not necessary, but how should look the object after *sorting* and what have you tried? – Nina Scholz Mar 01 '18 at 14:12
  • Have a look here [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – kevinSpaceyIsKeyserSöze Mar 01 '18 at 14:15
  • I was really hoping to show what I have so far, but I honestly don't have anything. Primarily because I'm not even sure how to sort based date @NinaScholz – Doe Mar 01 '18 at 14:25

3 Answers3

0

This outputs the messages sorted most recent first as an array... you could rebuild the object if you really need to.

const json = {
    "Messages": {
        "message1": {
            "msg"       : "I'm trying to make this work",
            "timeStamp" : "2018-02-15T06:24:44.12+00:00"
        },
        "message2": {
            "msg"       : "I really need your help SO!",
            "timeStamp" : "2018-03-01T13:57:27+00:00"  
        },
        "message3": {
            "msg"       : "Please assist me dude!",
            "timeStamp" : "2018-03-01T11:57:27+00:00"
        }
    }
}

const messageIds = Object.keys(json.Messages)

const messages = messageIds.map(id => json.Messages[id]).sort((a, b) =>
    a.timeStamp < b.timeStamp ? 1 : -1
)

console.log(JSON.stringify(messages, null, 4))
phuzi
  • 12,078
  • 3
  • 26
  • 50
john_omalley
  • 1,398
  • 6
  • 13
0

you can revive you dates as follows: If you messages are in a variable: foo

foo =  {
  "message1": {
    "msg"       : "I'm trying to make this work",
    "timeStamp" : "2018-02-15T06:24:44.12+00:00"
  },
  "message2": {
    "msg"       : "I really need your help SO!",
    "timeStamp" : "2018-03-01T13:57:27+00:00"  
  },
  "message3": {
    "msg"       : "Please assist me dude!",
    "timeStamp" : "2018-03-01T11:57:27+00:00"
  }
}

Then this will add a new property wihch you can sort on (datetime)

foo = Object.keys(foo).map(key => { return Object.assign({}, foo[key], {datetime: new Date(foo[key].timeStamp)}) });

you can get the messages in order as follows:

const sortedMessges = Object.keys(foo).map(key => foo[key]).sort((a, b) =>
    a.datetime< b.datetime ? 1 : -1)
developer
  • 690
  • 7
  • 16
0

You can sort and then map the sorted array.

var obj = {"Messages": {  "message1": {    "msg"       : "I'm trying to make this work",    "timeStamp" : "2018-02-15T06:24:44.12+00:00"  },  "message2": {    "msg"       : "I really need your help SO!",    "timeStamp" : "2018-03-01T13:57:27+00:00"    },  "message3": {    "msg"       : "Please assist me dude!",    "timeStamp" : "2018-01-01T11:57:27+00:00"  }}};

var result = Object.keys(obj.Messages)
                   .sort((a, b) => Date.parse(obj.Messages[a].timeStamp) - Date.parse(obj.Messages[b].timeStamp))
                   .map(k => ({ [k]: obj.Messages[k] }) );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Ele
  • 33,468
  • 7
  • 37
  • 75