Skip to content

Commit

Permalink
Sort data using locale in data tables (home-assistant#17781)
Browse files Browse the repository at this point in the history
* Sort data using locale in data tables

* Only use string compare is both values are string
  • Loading branch information
piitaya authored Sep 1, 2023
1 parent 5da4e18 commit fe3a63a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/components/data-table/ha-data-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@ export class HaDataTable extends LitElement {
filteredData,
this._sortColumns[this._sortColumn],
this._sortDirection,
this._sortColumn
this._sortColumn,
this.hass.locale.language
)
: filteredData;

Expand Down
15 changes: 6 additions & 9 deletions src/components/data-table/sort-filter-worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// To use comlink under ES5
import "proxy-polyfill";
import { expose } from "comlink";
import "proxy-polyfill";
import { stringCompare } from "../../common/string/compare";
import type {
ClonedDataTableColumnData,
DataTableRowData,
Expand Down Expand Up @@ -39,7 +40,8 @@ const sortData = (
data: DataTableRowData[],
column: ClonedDataTableColumnData,
direction: SortingDirection,
sortColumn: string
sortColumn: string,
language?: string
) =>
data.sort((a, b) => {
let sort = 1;
Expand All @@ -58,13 +60,8 @@ const sortData = (
if (column.type === "numeric") {
valA = isNaN(valA) ? undefined : Number(valA);
valB = isNaN(valB) ? undefined : Number(valB);
} else {
if (typeof valA === "string") {
valA = valA.toUpperCase();
}
if (typeof valB === "string") {
valB = valB.toUpperCase();
}
} else if (typeof valA === "string" && typeof valB === "string") {
return sort * stringCompare(valA, valB, language);
}

// Ensure "undefined" and "null" are always sorted to the bottom
Expand Down
6 changes: 4 additions & 2 deletions src/components/data-table/sort-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ export const filterData = (
filter: FilterDataParamTypes[2]
): Promise<ReturnType<FilterDataType>> =>
getWorker().filterData(data, columns, filter);

export const sortData = (
data: SortDataParamTypes[0],
columns: SortDataParamTypes[1],
direction: SortDataParamTypes[2],
sortColumn: SortDataParamTypes[3]
sortColumn: SortDataParamTypes[3],
language?: SortDataParamTypes[4]
): Promise<ReturnType<SortDataType>> =>
getWorker().sortData(data, columns, direction, sortColumn);
getWorker().sortData(data, columns, direction, sortColumn, language);

0 comments on commit fe3a63a

Please sign in to comment.