Skip to content

fix: respect SORT direction when sorting GROUP BY keys - #2666

Open
hgabreu wants to merge 1 commit into
blacksmithgu:masterfrom
hgabreu:fix/group-by-sort-direction
Open

fix: respect SORT direction when sorting GROUP BY keys#2666
hgabreu wants to merge 1 commit into
blacksmithgu:masterfrom
hgabreu:fix/group-by-sort-direction

Conversation

@hgabreu

@hgabreu hgabreu commented Apr 24, 2026

Copy link
Copy Markdown

Problem

When a GROUP BY is followed by SORT ... DESC, the group keys are always sorted ascending. This is because the internal sort inside executeCore() was hardcoded to ascending order, regardless of the subsequent SORT operation.

Queries like:

table length(rows) as "Count"
from #book
where status = "read"
group by dateformat(date(date_read), "yyyy-MM") as "Year-Month"
sort date(date_read) desc

…produce groups in ascending order instead of descending.

Related issues: #966, #1671

Fix

In executeCore(), changed the for...of loop to an indexed loop so the current position in the ops array is known. When processing a group op, look ahead for an immediately following sort op and read its first field's direction. Use that to set a sort factor (-1 for descending, +1 for ascending/default) applied to the group key comparator.

Test

Verified locally by deploying the built plugin to an Obsidian vault and confirming that GROUP BY ... SORT ... DESC now produces groups in descending order.

🤖 Generated with Claude Code

When a GROUP BY is followed by a SORT ... DESC, the group keys were
always sorted ascending because the internal sort in executeCore() was
hardcoded to ascending order.

Fix: look ahead in the ops array from the GROUP BY step for an
immediately following SORT operation and use its first field's direction
to set the sort factor (-1 for descending, +1 for ascending/default).

Fixes behaviour reported in issues blacksmithgu#966 and blacksmithgu#1671.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant