Can someone help me simplify my sorting function for array columns? Currently, I have multiple functions like the one below for each column:
sort() {
if (this.sortAsc == false) {
this.tab.sort((a, b) => {
return a.name.localeCompare(b.name);
});
this.sortAsc = true;
} else {
this.tab.sort((a, b) => {
}
return b.name.localeCompare(a.name);
});
this.sortAsc = false;
}
}
An excerpt from the table structure:
...
<tr>
<th (click)="sort()">Name</th>
...
</tr>
</thead>
<tbody>
<tr *ngFor="let item of tab" (click)="getById(item.id)" tabindex="0">
<td> {{ item.name }} </td>
...
</tr>
...