From e242fbe14801011ed6f0c7a23eaa5f3173518b1b Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Mon, 23 Jan 2023 12:10:01 -0800 Subject: [PATCH] Sort battery level column using numeric sort (#14926) fixes undefined --- src/components/data-table/ha-data-table.ts | 1 - src/components/data-table/sort_filter_worker.ts | 15 ++++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/components/data-table/ha-data-table.ts b/src/components/data-table/ha-data-table.ts index a9d9bf3ea0..dad2d26efb 100644 --- a/src/components/data-table/ha-data-table.ts +++ b/src/components/data-table/ha-data-table.ts @@ -200,7 +200,6 @@ export class HaDataTable extends LitElement { Object.values(clonedColumns).forEach( (column: ClonedDataTableColumnData) => { delete column.title; - delete column.type; delete column.template; } ); diff --git a/src/components/data-table/sort_filter_worker.ts b/src/components/data-table/sort_filter_worker.ts index 286bae3014..36e756f91e 100644 --- a/src/components/data-table/sort_filter_worker.ts +++ b/src/components/data-table/sort_filter_worker.ts @@ -55,11 +55,16 @@ const sortData = ( ? b[column.valueColumn || sortColumn][column.filterKey] : b[column.valueColumn || sortColumn]; - if (typeof valA === "string") { - valA = valA.toUpperCase(); - } - if (typeof valB === "string") { - valB = valB.toUpperCase(); + 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(); + } } // Ensure "undefined" is always sorted to the bottom