Sort mat-angular-table is prioritizing uppercase letters

Asked

Viewed 56 times

0

I’m using the mat Sort to sort a mat-table at the angle, the problem is that when I sort upwards the algorithm is taking uppercase letters as a priority, so if I have a table with the following names:

"Marcos"
"antonio"

Mark appears first in ordination.

This is the algorithm you use to sort:

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)">

1 answer

0

I was able to settle it by capitalizing before comparing:

 return this.compare(a.nome.toUpperCase(), b.nome.toUpperCase(), isAsc);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.