mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-24 09:46:36 +00:00
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:
parent
5da4e1860a
commit
fe3a63af80
@ -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;
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user