Sorting in Dataview not working for the first line

I’m still new to Dataview and not familiar with JavaScript. Asking ChatGPT how to achieve what I need, I got this, which works when it comes to structure, but all first lines inside each group shows unsorted:

let pages = dv.pages().where(p => p.Month);
let groups = pages.groupBy(p => p.Month).sort(g => g.key);

for (let g of groups) {
    let sorted = g.rows.sort((a, b) => {
        let nameA = a.file?.path ?? ""; 
        let nameB = b.file?.path ?? "";
        return nameA.localeCompare(nameB);
    });

    dv.header(3, `${g.key} (${sorted.length})`);
    dv.list(sorted.map(r => r.file ? dv.fileLink(r.file.path) : "(No file)"));
}

I’m getting this:

So, my notes have a property called Month and each value is 01 Jan, 02 Feb, etc.

image

I want it grouped by month, show the number of notes per month next to the name, then show the list of notes inside that property value and sort the notes.

Everything is working, it’s just that first line, inside all months, that show unsorted.

I went back and forth with ChatGPT and Claude, can’t make it work.

Any tips?
Thanks!

It’s working!

Right after I posted here, I decided to ask it one more time and Claude gave me this:

let pages = dv.pages().where(p => p.Month);
let groups = pages.groupBy(p => p.Month).sort(g => g.key);
for (let g of groups) {
    let sorted = [...g.rows].sort((a, b) => {
        let nameA = a.file?.path ?? ""; 
        let nameB = b.file?.path ?? "";
        return nameA.localeCompare(nameB);
    });
    dv.header(3, `${g.key} (${sorted.length})`);
    dv.list(sorted.map(r => r.file ? dv.fileLink(r.file.path) : "(No file)"));
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.