order by ascending descending in angular 6 on click of button

Solutions on MaxInterview for order by ascending descending in angular 6 on click of button by the best coders in the world

showing results for - "order by ascending descending in angular 6 on click of button"
Wesley
04 Nov 2016
1private isAscendingSort: boolean = false;
2
3sortUser() {
4  console.log('sorting!'); // just to check if sorting is being called
5  this.isAscendingSort = !this.isAscendingSort; // you missed this
6
7  this.items.sort((item1: any, item2: any) => this.compare(item1, item2));
8}
9
Maximilian
16 Feb 2018
1let isAscendingSort: Boolean = true;
2sortUser() {
3    console.log('sorting!'); //just to check if sorting is beng called
4    this.items.sort((item1: any, item2: any) => this.compare(item1, item2));
5  }
6  // Sort
7  compare(item1: any, item2: any): number {
8    let compValue = 0;
9      compValue = item1.attributes.fullName.localeCompare(item2.attributes.fullName, 'en', {
10        sensitivity: 'base'
11      });
12    console.log(compValue);
13    if (!this.isAscendingSort) {
14      compValue = compValue * -1;
15    }
16    return compValue;
17  }
Samson
29 Apr 2019
1<button (click)="sortData()">Sort Data</button>
2<div *ngFor="let item of items">
3{{items.attributes.fullName}}
4</div>