Skip to content

Commit d9b2e56

Browse files
authored
Merge pull request #171 from ibdafna/mixed_dtypes
Enhance sorting logic for mixed type columns
2 parents f54fdac + c7b695a commit d9b2e56

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/core/transformExecutors.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,44 @@ 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;
251252

252-
if (this._options.desc) {
253-
sortFunc = (a: any, b: any): number => {
254-
return a[field] < b[field] ? 1 : -1;
255-
};
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') {
260+
return String(value);
261+
}
262+
return value;
263+
};
264+
265+
if (columnDataType == 'string') {
266+
if (this._options.desc) {
267+
sortFunc = (a: any, b: any): number => {
268+
return stringifyIfNeeded(a[field]) < stringifyIfNeeded(b[field])
269+
? 1
270+
: -1;
271+
};
272+
} else {
273+
sortFunc = (a: any, b: any): number => {
274+
return stringifyIfNeeded(a[field]) > stringifyIfNeeded(b[field])
275+
? 1
276+
: -1;
277+
};
278+
}
256279
} else {
257-
sortFunc = (a: any, b: any): number => {
258-
return a[field] > b[field] ? 1 : -1;
259-
};
280+
if (this._options.desc) {
281+
sortFunc = (a: any, b: any): number => {
282+
return a[field] < b[field] ? 1 : -1;
283+
};
284+
} else {
285+
sortFunc = (a: any, b: any): number => {
286+
return a[field] > b[field] ? 1 : -1;
287+
};
288+
}
260289
}
261290

262291
const data = input.data.slice(0);

src/tests/transformExecutors.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const sortTestCases: Private.SortTestCase[] = [
2525
{ desc: false, dType: 'datetime', data: ['2019-09-12T18:38:47.431Z', '2019-09-07T18:38:47.431Z', '2019-09-10T18:38:47.431Z'], expected: ['2019-09-07T18:38:47.431Z', '2019-09-10T18:38:47.431Z', '2019-09-12T18:38:47.431Z'] },
2626
{ desc: false, dType: 'datetime', data: ['2019-09-12T18:38:47.431Z', null, '2019-09-10T18:38:47.431Z'], expected: ['2019-09-10T18:38:47.431Z', '2019-09-12T18:38:47.431Z', null] },
2727
{ desc: false, dType: 'datetime', data: ['2019-09-12T18:38:47.431Z', INVALID_DATE, '2019-09-10T18:38:47.431Z'], expected: ['2019-09-10T18:38:47.431Z', '2019-09-12T18:38:47.431Z', INVALID_DATE] },
28+
// Mixed types (treated as strings)
29+
{ desc: false, dType: 'string',
30+
data: [1, Number.NaN, 'B', '2019-09-10T18:38:47.431Z', 101.22, 1.1,Number.NaN, 1.21, 1.31, Number.NaN, Number.NaN, 1.11, 1.21, 1.91, 'A', 9.76],
31+
expected: [1, 1.1, 1.11, 1.21, 1.21, 1.31, 1.91, 101.22, '2019-09-10T18:38:47.431Z', 9.76, 'A', 'B', Number.NaN, Number.NaN, Number.NaN, Number.NaN] },
2832
];
2933

3034
// Run tests

0 commit comments

Comments
 (0)