Dataview sort various date formats

What I’m trying to do

I’m looking to sort various date formats in a reverse chronological order in an Dataview table. There would be the 3 possible date formats: yyyy-MM-dd, yyyy-MM or yyyy.

This is what it looks like right now:

As you can see, in the “Vu” column, the yyyy date format comes to the top even if the date is actually anterior to the first yyyy-MM-dd date.

Things I have tried

I have tried the Sort choice option as it was suggested on the forum, but I am a newbie in programming and I’m having a hard time merging Date Format options to Sort choice in Dataview. Also tried dataviewjs, to no avail…

Does anyone know how to do this?

Thanks in advance!

Only two of those three are recognised as dates. The one with just the year is just a number, and requires special treatment to be recognised as a date.

Some have been satisfied with doing string(Vu) and using that for sorting, as that converts everything into strings, which with these formats should yield the correct order.

Thank you @holroy!

Worked like a charm for yyyy-MM-dd and yyyy when I changed some instances of “Vu” for string(Vu), but somehow yyyy-MM now gets automatically turned into yyyy-MM-dd.
Example: 2023-05 will be displayed 2023-05-01…

This is the code I have:

Table WITHOUT ID 
Réalisateur, file.link AS "Titre", Type, string(Vu) AS "Vu"
from "Sources/Films"
FLATTEN Vu AS Vu
Sort string(Vu) DESCENDING, Réalisateur ASCENDING, file.link DESC

Let me know if you have an idea on how to fix this!

Since the yyyy-MM is actually turned into a date, it’ll get the “-01” appended, whether we like it or not. Don’t know how to easily remove it, or get access to the raw value unless your values are in the frontmatter where it might be reachable through file.frontmatter.Vu or similar.

That’s a bit too advanced for me :laughing:

Thank you so much for helping, I’m happy enough with string(Vu)!

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