Is there a way to implement sorting in both ascending and descending order? I currently have code that only allows for sorting in ascending order by name and identifier. How can I modify it to also support sorting in descending order?
sortType(sort: string){
if(sort === 'id') {
this.projects = this.projects.sort(this.sortByID);
console.log(this.projects);
}
if(sort === 'name') {
this.sprWellpads = this.projects.sort(this.sortByName);
console.log(this.projects);
}
}
sortByName(a: Project, b: Project){
if(a.name > b.name) return 1
else if (a.name == b.name)return 0
else return -1
}
sortByID(a: Project, b: Project){
return parseInt(a.id.toString())-parseInt(b.id.toString());
}
html:
<a class="sorting" (click)="sortType('id')" [class.active]="sortBy === 'id'" ><a class="title">ID⬇</a></a>
<a class="sorting" (click)="sortType('name')" [class.active]="sortBy === 'name'"><a class="title">Name⬇</a></a>