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

1

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.