Code to list PDFs in a folder in Numerical / Alphabetical order

What I’m trying to do

I’m trying to list Pdf documents from a specific folder in my vault alphabetically / Numerically.

Things I have tried

I am using the following code which I found somewhere in this forum some time ago.

```dataviewjs
const pdfFiles = app.vault.getFiles().filter(file => file.extension === 'pdf' && file.path.includes('F8 AUDIT & ASSURANCE/OPEN TUITION/F8 AA PDFs'))
dv.list(pdfFiles.map(file => dv.fileLink(file.path)))

and this gives the following output which seems to be random:

I’d really like the code to return the results in alphabetical or numerical order but I have no idea how to code for this!

Any help appreciated :grinning:

Thanks

I guess you’re missing .sort between const and dv.list, Try this …

const pdfFiles = app.vault.getFiles()
    .filter(file => file.extension === 'pdf' && file.path.includes('F8 AUDIT & ASSURANCE/OPEN TUITION/F8 AA PDFs'))
    .sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' }));

dv.list(pdfFiles.map(file => dv.fileLink(file.path)));

Cheers, Marko :nerd_face:

Edited on 04.03.2025 as @neonate point it out - thx!

I am not the OP but this worked fine for me. I had to make 1 change: on the last line, I had to delete the space in: (file.path) - I also make the “P” in path lower case (don’t know if this is case sensitive).

Thank you so much for posting. I have been searching for this answer for quite a while. Here’s a topic I posted: DataviewJS to show pdfs - how to sort by asc?

May I post your answer as a solution and attribute to you?

1 Like

Oh, haven’t noticed this big mistake! I guess … as I’m not a native English speaker … that my spellchecker “fixed” grammatically the code :slight_smile:

What you did is correct, and thx for pointing it out. Will fix it now. And yes, share and use it wherever it can be helpful.

Cheers, Marko :nerd_face:

1 Like

Thanks to Marko and neonate.

This is working great now :+1:

This is the final version of the code: (After the changes neonate made)

dataviewjs
const pdfFiles = app.vault.getFiles() .filter(file => file.extension === 'pdf' && file.path.includes('F8 AUDIT & ASSURANCE/OPEN TUITION/F8 AA PDFs')) .sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' })); dv.list(pdfFiles.map(file => dv.fileLink(file.path)));

:+1:

1 Like