Sort data using locale in data tables (#17781)

* Sort data using locale in data tables

* Only use string compare is both values are string
This commit is contained in:
Paul Bottein 2023-09-01 17:33:41 +02:00 committed by GitHub
parent 5da4e1860a
commit fe3a63af80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 12 deletions

View File

@ -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;

View File

@ -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,
@ -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;
@ -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

View File

@ -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);