I have used column sorting on table. It's working fine on first name and last name column but on dl and dl score it is not working. Can you look into it and help me fix it.
You can view here: https://stackblitz.com/edit/angular-ivy-87hi8i?file=src%2Fapp%2Fapp.component.html
sort(property: any) {
this.isDesc = !this.isDesc;
this.column = property;
let direction = this.isDesc ? 1 : -1;
this.allUser.sort(function(
a: { [x: string]: number },
b: { [x: string]: number }
) {
if (a[property] < b[property]) {
return -1 * direction;
} else if (a[property] > b[property]) {
return 1 * direction;
} else {
return 0;
}
});
}
markup
<tr>
<th *ngIf="!isEdit">Edit</th>
<th [ngClass]="{pointer: true, active:column=='first_name',desc:isDesc, asc:!isDesc}"
(click)="sort('first_name')">First Name</th>
<th [ngClass]="{pointer: true, active:column=='last_name',desc:isDesc, asc:!isDesc}"
(click)="sort('last_name')">Last Name</th>
<th>Email</th>
<th>Gender</th>
<th>DOB</th>
<th>Impact</th>
<th>Score</th>
<th [ngClass]="{pointer: true, active:column=='dl',desc:isDesc, asc:!isDesc}" (click)="sort('dl')">
DL</th>
<th [ngClass]="{pointer: true, active:column=='co_score',desc:isDesc, asc:!isDesc}"
(click)="sort('co_score')">DL Score</th>
<th></th>
</tr>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…