Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.

fix sorting missing values position #136

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/dataframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -1006,9 +1006,12 @@ class DataFrame {
return _columnNames
.map((col) => {
const [pValue, nValue] = [p.get(col), n.get(col)];
if (_checkMissingValue(pValue)) {
const [pMissing, nMissing] = [_checkMissingValue(pValue),_checkMissingValue(nValue)];
if (pMissing && nMissing) {
return 0;
} else if (pMissing) {
return _missingValuesPosition === "last" ? 1 : -1;
} else if (_checkMissingValue(nValue)) {
} else if (nMissing) {
return _missingValuesPosition === "last" ? -1 : 1;
} else if (typeof pValue !== typeof nValue) {
throw new MixedTypeError([
Expand Down
31 changes: 31 additions & 0 deletions tests/sort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import test from "ava";

import DataFrame from "../src/dataframe";

test("Sort multiple cols with undefined ", assert => {
const df = new DataFrame(
[
{ col1: 1, col2: "A", },
{ col1: 2, col2: undefined },
{ col1: 3, col2: "B" },
{ col1: undefined, col2: "On the road" },
{ col1: undefined, col2: undefined },
{ col1: undefined, col2: "C" }
]
);

const sorted = df.sortBy(['col1', 'col2'], false, 'last');

assert.deepEqual(
sorted.toCollection(),
[
{ col1: 1, col2: 'A' },
{ col1: 3, col2: 'B' },
{ col1: 2, col2: undefined },
{ col1: undefined, col2: 'C' },
{ col1: undefined, col2: 'On the road' },
{ col1: undefined, col2: undefined },
],
"sort multiple undef cols"
);
});