1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 09:13:33 +00:00

[SM-552] make string sort in TableDataSource case insensitive (#4889)

* make string sort case insensitive

* use localeCompare
This commit is contained in:
Will Martin
2023-03-01 14:41:56 -05:00
committed by GitHub
parent ce6c975dd9
commit bb36184256
2 changed files with 35 additions and 3 deletions

View File

@@ -132,7 +132,7 @@ export class TableDataSource<T> extends DataSource<T> {
*/
protected sortData(data: T[], sort: Sort): T[] {
const column = sort.column;
const direction = sort.direction;
const directionModifier = sort.direction === "asc" ? 1 : -1;
if (!column) {
return data;
}
@@ -140,7 +140,7 @@ export class TableDataSource<T> extends DataSource<T> {
return data.sort((a, b) => {
// If a custom sort function is provided, use it instead of the default.
if (sort.fn) {
return sort.fn(a, b) * (direction === "asc" ? 1 : -1);
return sort.fn(a, b) * directionModifier;
}
let valueA = this.sortingDataAccessor(a, column);
@@ -161,6 +161,10 @@ export class TableDataSource<T> extends DataSource<T> {
}
}
if (typeof valueA === "string" && typeof valueB === "string") {
return valueA.localeCompare(valueB) * directionModifier;
}
// If both valueA and valueB exist (truthy), then compare the two. Otherwise, check if
// one value exists while the other doesn't. In this case, existing value should come last.
// This avoids inconsistent results when comparing values to undefined/null.
@@ -179,7 +183,7 @@ export class TableDataSource<T> extends DataSource<T> {
comparatorResult = -1;
}
return comparatorResult * (direction === "asc" ? 1 : -1);
return comparatorResult * directionModifier;
});
}