diff --git a/libs/components/src/table/table-data-source.ts b/libs/components/src/table/table-data-source.ts index b7bb4d1644e..71e8b8318e3 100644 --- a/libs/components/src/table/table-data-source.ts +++ b/libs/components/src/table/table-data-source.ts @@ -132,7 +132,7 @@ export class TableDataSource extends DataSource { */ 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 extends DataSource { 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 extends DataSource { } } + 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 extends DataSource { comparatorResult = -1; } - return comparatorResult * (direction === "asc" ? 1 : -1); + return comparatorResult * directionModifier; }); } diff --git a/libs/components/src/table/table.stories.ts b/libs/components/src/table/table.stories.ts index 76723f23d10..dbf421f4eee 100644 --- a/libs/components/src/table/table.stories.ts +++ b/libs/components/src/table/table.stories.ts @@ -168,3 +168,31 @@ const FilterableTemplate: Story = (args) => ({ }); export const Filterable = FilterableTemplate.bind({}); + +const data4 = new TableDataSource<{ name: string }>(); + +data4.data = [...Array(5).keys()].map((i) => ({ + name: i % 2 == 0 ? `name-${i}`.toUpperCase() : `name-${i}`.toLowerCase(), +})); + +const VariableCaseTemplate: Story = (args) => ({ + props: { + dataSource: data4, + }, + template: ` + + + + Name + + + + + {{ r.name }} + + + + `, +}); + +export const VariableCase = VariableCaseTemplate.bind({});