I'm trying to modify this code so that it can sort both A-Z and Z-A using a single button. The current code only sorts from A-Z and doesn't work in reverse order. Here is the code I have, but it's not functioning correctly.
sortType(sort: string, order: string){
if(sort === 'name') {
this.projects = this.projects.sort(this.sortByCountryName)
console.log(this.sprBitTypes);
}
else {this.projects = this.projects.sort(this.sortByCountryName1);}
}
sortByCountryName(c1: SprBitType, c2:SprBitType){
if(c1.name > c2.name) return 1
else if (c1.name == c2.name)return 0
else return -1
}
sortByCountryName1(c1: SprBitType, c2:SprBitType){
if(c1.name < c2.name) return 1
else if (c1.name == c2.name)return 0
else return -1
}
html:
<a class="sort" (click)="sortType('name')" [class.active]="sortBy === 'name'" >name</a>