0

Its my data from api

{
    id: '1',
    developer: {
        name: 'Michelle Stewart',
        proff: 'Account',
        shortInfo: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,',
        mail: 'michellestewart@gmail.com',
        phone: '+48 500 400 300',
        adress: '65 Lorem St, Warsaw, PL',
        company: 'Symu.co',
        isOnline: false
    },
    time: 'Today, 5:32 PM',
    devmessages: [
        {
            time: '10 April 2018',
            message: 'Lorem ipsum dolor sit amet, consectetur'
        },
        {
            time: '10 April 2018',
            message: 'Lorem ipsum dolor sit amet, consectetur'
        },
        {
            time: '10 April 2018',
            message: 'quis nostrud exercitation ullamco laboris'
        }
    ],
    usermessages: [
        {
            time: '10 April 2018',
            message: 'Oops ops)',
            user: true

        },
        {
            time: '10 April 2018',
            message: 'Hi how do you do?',
            user: true
        },
        {
            time: '10 April 2018',
            message: 'Can u test my code',
            user: true
        }
    ],
    isUnread: true
},

and i have React component

<ul className="MessagesField">
                    {
                        this.props.data !== '' && this.props.data[0].devmessages.concat(this.props.data[0].usermessages).map((item, index) => {
                            return <li key={index} className={ item.user ? 'MessageFiled UserMessage' : 'MessageFiled ConversationMessage'}>
                                <div className="UserAvatar">
                                    <img src={ item.user === true ? this.setUserImg(this.state.userId) : this.setConversationsImg(this.props.data[0].id)}/>
                                </div>
                                <div className="MessageBody">
                                    <p>{item.message}</p>
                                    <span>{item.time}</span>
                                </div>
                            </li>
                        })
                    }
                </ul>

now i have this result http://i.prntscr.com/Mk3CwGzhQL2nGRW8_nZ-Cg.png

but i need sort my messages by date as i understand i need parse my data to date with moment.js and next sort by this date. I dont know how to do it. Can someone help me please) In result i need get http://i.prntscr.com/neLdi4r3RgS9OEfjtUJOzQ.png sort messages by date of massages

  • 1
    Possible duplicate of [Sort Javascript Object Array By Date](https://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date) – jmargolisvt Apr 04 '18 at 20:30

2 Answers2

2

I would change the way you concatenate the arrays, and use array.sort to sort the messages:

[ ...this.props.data[0].devmessages, ...this.props.data[0].usermessages]
.sort( (a, b) => new Date(b.time) - new Date(a.time) )
.map( item => (
  <li key=...................... /li>
)

Note that your API is returning just the day, and not the time of the day, so the messages won't be sorted unless you change the API. Also I would recommend that the API concatenates the messages arrays and sorts it. For writing the time on the json I recommend using the ISO8601 Date and Time Format to pass the date and time from the api to your application

Murilo Cruz
  • 1,819
  • 13
  • 18
0

You can sort even without momentjs. Momentjs is used to parse and format the timestamp that you give. You can convert time into a integerby using new Date(your_time_object).getTime();

There are many ways you can implement this. you can use the below code for eg:

const getAllMessages = () => {
   let all_messages = [...this.props.data[0].devmessages, ...this.props.data[0].usermessages];
   all_messages = all_messages.map(messages => {
       message.new_time = new Date(message.time).getTime();
   }).sort((first_value, second_value) => {
       return first_value.new_time - second_value.new_time;
   });

   this.setState({ 
       all_messages
   });
}

Now inside render method

<ul className="MessagesField">
    {this.state.all_message.length && this.state.all_message.map((item, index) => {
       return ( 
           <li key={index} className={ item.user ? 'MessageFiled UserMessage' : 'MessageFiled ConversationMessage'}>
             <div className="UserAvatar">
                 <img src={ item.user === true ? this.setUserImg(this.state.userId) : this.setConversationsImg(this.props.data[0].id)}/>
             </div>
             <div className="MessageBody">
                 <p>{item.message}</p>
                 <span>{item.time}</span>
              </div>
          </li>
       );
    })}
 </ul>

I havent tested this code myself, but this should work. And ofcourse, all your timestamps are smae. it has to be more precise, with a time too if possible to get better results

Noushad
  • 3,506
  • 2
  • 20
  • 26