Skip to content
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
16 changes: 10 additions & 6 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,20 @@ export const sort = (
}
}

const sorted = collection.slice().sort((a, b) => {
return collection.slice().sort((a, b) => {
for (let i = 0, l = orders.length; i < l; i += 1) {
const [keys, desc] = orders[i];
const aValue = getValue(a, keys);
const bValue = getValue(b, keys);

// Handle undefined values for sorting
const aUndef = typeof aValue === "undefined";
const bUndef = typeof bValue === "undefined";
// eslint-disable-next-line no-continue
if (aUndef && bUndef) continue;
if (aUndef) return desc ? 1 : -1; // undefined at bottom for descending, top for ascending
if (bUndef) return desc ? -1 : 1;

const r = comparator(aValue, bValue);
if (r !== 0) return desc ? -r : r;
}
Expand All @@ -257,11 +266,6 @@ export const sort = (
const rid2 = getRid(b);
return comparator(rid1, rid2);
});

if (orders.length !== 1) return sorted;

const [keys] = orders[0];
return sorted.filter(d => typeof getValue(d, keys) !== "undefined");
};

export const paginate = (
Expand Down
20 changes: 16 additions & 4 deletions test/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,11 @@ export const orderByWithEmptyNestedProperty = testQuery(
query: "select * from c order by c.child.name",
parameters: [{ name: "@name", value: "foo" }]
},
[{ id: "foo", child: { name: "foo" } }]
[
{ id: "bar", child: null },
{ id: "baz" },
{ id: "foo", child: { name: "foo" } }
]
);

export const deeplyNestedProperty = testQuery(
Expand Down Expand Up @@ -797,12 +801,20 @@ export const multipleOrderByWithCompositeIndexes2 = testQuery(
]
);

export const filterUndefinedOrderBy = testQuery(
export const filterUndefinedOrderByASC = testQuery(
[{ id: "foo", sortKey: "a" }, { id: "bar" }, { id: "baz", sortKey: "b" }],
{
query: "SELECT * FROM c ORDER BY c.sortKey ASC"
},
[{ id: "bar" }, { id: "foo", sortKey: "a" }, { id: "baz", sortKey: "b" }]
);

export const filterUndefinedOrderByDESC = testQuery(
[{ id: "foo", sortKey: "a" }, { id: "bar" }, { id: "baz", sortKey: "b" }],
{
query: "SELECT * FROM c ORDER BY c.sortKey"
query: "SELECT * FROM c ORDER BY c.sortKey DESC"
},
[{ id: "foo", sortKey: "a" }, { id: "baz", sortKey: "b" }]
[{ id: "baz", sortKey: "b" }, { id: "foo", sortKey: "a" }, { id: "bar" }]
);

export const DoNotfilterUndefinedMultipleOrderBy = testQuery(
Expand Down