From 06aac469145683af5790fe2b8e1c426b9b18c2c0 Mon Sep 17 00:00:00 2001 From: Angus Lee Date: Wed, 14 Aug 2024 14:47:06 +0800 Subject: [PATCH] Issue #1424 Sorting with Undefined Values. --- package.json | 1 + src/pipeline/sort/native.ts | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 012e1b79..87c7e651 100644 --- a/package.json +++ b/package.json @@ -123,6 +123,7 @@ "test:watch": "jest --watch" }, "dependencies": { + "fast-sort": "^3.4.1", "preact": "^10.10.6" }, "changelog": { diff --git a/src/pipeline/sort/native.ts b/src/pipeline/sort/native.ts index 65d3a3d1..fd45ab1c 100644 --- a/src/pipeline/sort/native.ts +++ b/src/pipeline/sort/native.ts @@ -7,6 +7,7 @@ import { } from '../processor'; import Row from '../../row'; import log from '../../util/log'; +import { inPlaceSort, ISortByObjectSorter } from 'fast-sort'; interface NativeSortProps extends PipelineProcessorProps { columns: { @@ -67,7 +68,26 @@ class NativeSort extends PipelineProcessor { protected _process(data: Tabular): Tabular { const sortedRows = [...data.rows]; - sortedRows.sort(this.compareWrapper.bind(this)); + /* sortedRows.sort(this.compareWrapper.bind(this)); */ + let sortBys: ISortByObjectSorter[] = []; + for (const column of this.props.columns) { + let sortBy: ISortByObjectSorter = {}; + if (column.direction === -1) { + sortBy.desc = (r: Row): TCell => { + return r.cells[column.index].data; + }; + } + else { + sortBy.asc = (r: Row): TCell => { + return r.cells[column.index].data; + }; + } + if (typeof column.compare === 'function') { + sortBy.comparer = column.compare; + } + sortBys.push(sortBy); + } + inPlaceSort(sortedRows).by(sortBys); const sorted = new Tabular(sortedRows); // we have to set the tabular length manually