Skip to content

Commit 0db2dfd

Browse files
committed
enhance sorting logic for mixed type columns
1 parent 0e0c1a8 commit 0db2dfd

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/core/transformExecutors.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,31 @@ export class SortExecutor extends TransformExecutor {
248248
public apply(input: TransformExecutor.IData): TransformExecutor.IData {
249249
let sortFunc: (a: any, b: any) => number;
250250
const field = this._options.field;
251+
const columnDataType = this._options.dType;
252+
253+
// Adding string checks within the sort function so we do
254+
// not have to mutate in-place the original types of the
255+
// values of the column into strings. This allows the
256+
// displayed values to maintain their original types but
257+
// be sorted as if they were all strings.
258+
const stringifyIfNeeded = (value: any) => {
259+
if (typeof value != 'string' && columnDataType == 'string') {
260+
return String(value);
261+
}
262+
return value;
263+
};
251264

252265
if (this._options.desc) {
253266
sortFunc = (a: any, b: any): number => {
254-
return a[field] < b[field] ? 1 : -1;
267+
return stringifyIfNeeded(a[field]) < stringifyIfNeeded(b[field])
268+
? 1
269+
: -1;
255270
};
256271
} else {
257272
sortFunc = (a: any, b: any): number => {
258-
return a[field] > b[field] ? 1 : -1;
273+
return stringifyIfNeeded(a[field]) > stringifyIfNeeded(b[field])
274+
? 1
275+
: -1;
259276
};
260277
}
261278

0 commit comments

Comments
 (0)