1

I have a set of data displaying. here the dates are not arranged in order.I want the data to be arranged in the descending order based on the dates.

HTML:

<mat-tab label="Active">
  <mat-icon for="search">search</mat-icon>
  <input type="search" [(ngModel)]="filter" name="search" class="search" placeholder="Company">
  <ul>
    <li *ngFor="let message of activeMessages| messagefilter: filter" (click)="showMessage(message)" [class.activeShow]="message.id == message_id">
      <span>{{message.messages[message.messages.length -1].updated_at  | date:'dd.MM.yyyy'}}</span>
      <img style="width: 40px;" [src]="message.from_user_image || '../assets/images/msg.png'"/>
      <p style="padding-top: 16px;display: inline;">{{message.from_user_name}}</p>
      <p style="padding-top: 10px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><b>{{message.messages[message.messages.length -1].text}}</b></p>
    </li>
  </ul>
</mat-tab>

TS:

loadMessages() {
    this.service
          .getMessages()
          .subscribe(
            data => {
              this.messagesdata = data;
              this.activeMessages = data.filter(msg => msg.active == true && msg.from_user_name !== 'Anonymus' && msg.messages.length > 0)
              this.closedMessages = data.filter(msg => msg.active == false && msg.from_user_name !== 'Anonymus' && msg.messages.length > 0);

            },error => {});
  }

enter image description here

here the date i am fetching is from messages array inside the data. So, can anyone help me to fix this issue

Bhrungarajni
  • 1,936
  • 3
  • 24
  • 53
  • This might help https://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date and this... https://stackoverflow.com/questions/41125311/sort-by-date-angular-2-pipe?rq=1 – Ric May 02 '18 at 10:18

1 Answers1

2

You can do something like this,

You can sort the this.activeMessages and the updated date of last message inside it

Your TS will become:

loadMessages() { 
    this.service 
    .getMessages() 
    .subscribe( 
    data => { 
        this.messagesdata = data; 
        this.activeMessages = data.filter(msg => msg.active == true && msg.from_user_name !== 'Anonymus' && msg.messages.length > 0) 
        this.activeMessages = this.activeMessages.sort((a: any, b: any) => new Date(b.messages[b.messages.length -1].updated_at).getTime() - new Date(a.messages[a.messages.length -1].updated_at).getTime() 
        this.closedMessages = data.filter(msg => msg.active == false && msg.from_user_name !== 'Anonymus' && msg.messages.length > 0); 
    },error => {}); 
}

Your HTML:

<mat-tab label="Active">
  <mat-icon for="search">search</mat-icon>
  <input type="search" [(ngModel)]="filter" name="search" class="search" placeholder="Company">
  <ul>
    <li *ngFor="let message of activeMessages| messagefilter: filter" (click)="showMessage(message)" [class.activeShow]="message.id == message_id">
      <span>{{message.messages[message.messages.length -1].updated_at  | date:'dd.MM.yyyy'}}</span>
      <img style="width: 40px;" [src]="message.from_user_image || '../assets/images/msg.png'"/>
      <p style="padding-top: 16px;display: inline;">{{message.from_user_name}}</p>
      <p style="padding-top: 10px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><b>{{message.messages[message.messages.length -1].text}}</b></p>
    </li>
  </ul>
</mat-tab>
Sravan
  • 16,897
  • 2
  • 24
  • 48