Issue
I'm trying to use the mat-sort to order one mat-table in angular, the problem is when i order in ascendent/decrescent order, the algoritm order capitalized words first than uncapitalized. If i have one table with this names:
"Mark" "antony"
"Mark" shows first than antony.
This is the algoritm:
sortData(sort: Sort) {
const data = this.descricoesProdutos.slice();
if (!sort.active || sort.direction === '') {
this.sortedData = data;
return;
}
this.sortedData = data.sort((a, b) => {
const isAsc = sort.direction === 'asc';
switch (sort.active) {
case 'nome': return this.compare(a.nome, b.nome, isAsc);
case 'descricao': return this.compare(a.descricao, b.descricao, isAsc);
default: return 0;
}
});
}
compare(a: number | string, b: number | string, isAsc: boolean) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}
My html:
<table matSort (matSortChange)="sortData($event)">
Solution
Make the names upper(or lower)case first, then compare them. This difference then will disappear.
return this.compare(a.nome.toUpperCase(), b.nome.toUpperCase(), isAsc);
Answered By - ForestG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.