Skip to content

Commit

Permalink
add NaN handling
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Feb 24, 2021
1 parent 7f0d906 commit 9b43ea2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ describe('Data sorting criteria', () => {
reverseOutput: false,
});
});

it(`should sort NaN to the end`, () => {
const now = Date.now();
testSorting({
input: [null, now, 0, undefined, Number.NaN, now - 150000],
output: [0, now - 150000, now, null, undefined, Number.NaN],
direction: 'asc',
type: 'number',
reverseOutput: false,
});

testSorting({
input: [null, now, 0, undefined, Number.NaN, now - 150000],
output: [now, now - 150000, 0, null, undefined, Number.NaN],
direction: 'desc',
type: 'number',
reverseOutput: false,
});
});
});

describe('String or anything else as string', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ function getUndefinedHandler(
return (rowA: Record<string, unknown>, rowB: Record<string, unknown>) => {
const valueA = rowA[sortBy];
const valueB = rowB[sortBy];
if (valueA != null && valueB != null) {
if (valueA != null && valueB != null && !Number.isNaN(valueA) && !Number.isNaN(valueB)) {
return sortingCriteria(rowA, rowB);
}
if (valueA == null) {
if (valueA == null || Number.isNaN(valueA)) {
return 1;
}
if (valueB == null) {
if (valueB == null || Number.isNaN(valueB)) {
return -1;
}

Expand Down

0 comments on commit 9b43ea2

Please sign in to comment.