Umaiz Khan
Umaiz Khan

Reputation: 1227

Ionic 4 how to show data time wise

I need help i have list of message in which there are 3 fields. message, uid & time. Its showing data but its mixed like when we enter data some goes in middle of array some goes in start some in end like this. But i need to show time wise mean the latest or newest time off array will come first or in end like this. Attach the data format also. This is simply how i am getting message from service

getAllMessages(){
    this.messageService.getAllMessages().subscribe((data)=>{
     this.data = data;
       console.log(this.data);
    });
}

Upvotes: 1

Views: 73

Answers (1)

Sergey Rudenko
Sergey Rudenko

Reputation: 9235

You need to use a pipe and map operator for this (map needs to be imported), that will then leverage sort method:

import { map } from 'rxjs/operators';

...

getAllMessages() {

  this.messageService.getAllMessages().pipe(

    map(messages => messages.sort((a:any, b:any) => b.time.seconds - a.time.seconds)

  ).subscribe(messages => this.data = messages)

}

I assumed you have seconds already so you do not have to transform date obj of any sort in this example. If it is not the case you can adjust this code accordingly I hope.

Upvotes: 1

Related Questions