I want to sort my journal daily notes by its title format (MMM Do, YYYY)

I want to sort my journal daily notes by its title format (MMM Do, YYYY).

I’ve written it like this:

```dataview
table without id
file.link as "All Daily Notes"
from #dailynotes
where file.name != "Daily Note Template"
SORT file.day DESC 
`` `

But when I write it like this, it doesn’t sort properly as you can see:

For example, it adds from this year (2024) as you can see in the image - Feb 1st, 2024

If I remember correctly, file.day only works for a date in the title following the ISO format (YYYY-MM-DD) … which is not the format you used :innocent:

Field Name Data Type Description
file.day Date Only available if the file has a date inside its file name (of form yyyy-mm-dd or yyyymmdd ), or has a Date field/inline field.

Source: Metadata on Pages - Dataview

You could try to use file.name instead but I have no idea if it’ll give you the expected results or not :no_mouth:

1 Like

Hi @Pch, thank you for responding. I really appreciate your help. Yeah, you’re right with file.day only working with the ISO format (YYYY-MM-DD). Do you know if there’s a way to make it work with my format (MMM Do, YYYY)?

I tried using file.name, but it doesn’t sort it right

So… one issue here, with the format you chose, is that Do (the ordinal notation for the day of month) exists in MomentJS … but not in Luxon which is the library Dataview uses for the dates/times/dates & times :sweat_smile: .

Meaning that I’m not sure how to “convert” that date into something Luxon could use :sweat_smile:

I know that it’s possible to use MomentJS within DataviewJS queries but I’m not so sure about DQL :no_mouth: .

This DataviewJS query should be the equivalent to the DQL you wrote but with the notes sorted according the date within the file.name in a descending order, using MomentJS to convert the dates from the format you used (MMM Do, YYYY) to ISO (YYYY-MM-DD) …

```dataviewjs
dv.table(
  ["All Daily Notes"],
  dv.pages("#dailynotes")
    .where(p => p.file.name != "Daily Note Template")
    .sort(p => moment(p.file.name.toString(), "MMM Do, YYYY").format("YYYY-MM-DD"), "desc")
	.map(p => [p.file.link])
)
```

Once MomentJS is involved though, there are probably other ways to make the sorting work :blush:

Something that could be easier, if you don’t want to deal with DataviewJS would be to add a date key to the Properties/YAML/Frontmatter of your daily notes, storing the date formatted following the ISO format and use that key to sort your daily notes in the query you wrote :innocent:

Edit: thinking about this, a whole lot of “string” manipulations could make the DQL work too :thinking:

1 Like

Wow @Pch , you are just amazing! Thank you for all the insight you just gave me! Now, I can make it work how I want it.

1 Like

Ok, so I don’t know if there would be an easier way :sweat_smile: … But I think I was able to make it work in DQL using RegEx to “rewrite” the date into a date Luxon can read (and therefore Dataview too) :upside_down_face:

```dataview
TABLE WITHOUT ID
  file.link as "All Daily Notes"
FROM #dailynotes
WHERE file.name != "Daily Note Template"
SORT date(regexreplace(file.name, "(\w{3}) (\d{1,2})(th|st|nd|rd), (\d{4})", "$4-$1-$2"), "yyyy-MMM-d") DESC
```

I’m absolutely no RegEx expert but in some kind of summary, the regexreplace():

regexreplace(file.name, "(\w{3}) (\d{1,2})(th|st|nd|rd), (\d{4})", "$4-$1-$2")

(Regex101 test)

Captures, from the file.name:

  1. Group 1 ($1): the “month part” composed of exactly 3 “word character” ( → (\w{3}))
  2. Group 2 ($2): 1 or 2 digits ( → (\d{1,2}))
  3. Group 3 ($3): Either the characters th or st or nd or rd ( → (th|st|nd|rd))
  4. Group 4 ($4): Exactly 4 digits ( → (\d{4}))

Which are then “re-ordered” as $4-$1-$2 which can somewhat be translated, following MomentJS syntax as YYYY-MMM-D, equivalent in Luxon to yyyy-MMM-d :sweat_smile: … which can then be parsed as a date Dataview/Luxon should be able to understand within the DQL and used to sort the results of the query :blush: .

And now that I wrote all this, I’m thinking that simply replacing the (th|st|nd|rd) by “blank” ("") could be largely enough :woman_facepalming: :laughing:

Well, I’m going to leave it, just in case :innocent: … but a simpler way to get to similar results in DQL could be:

```dataview
TABLE WITHOUT ID
  file.link as "All Daily Notes"
FROM #dailynotes
WHERE file.name != "Daily Note Template"
SORT date(regexreplace(file.name, "(th|st|nd|rd)", ""), "MMM d, yyyy") DESC
```

(Regex101 test is here but fairly untested with Dataview though :blush:)

2 Likes

Ahahah :smile: ! My pleasure :grin: !

I’m very glad to know that while I was absorbed into the RegEx Realms :laughing:, you were able to find a way to make it work as expected :raised_hands: :tada: !

1 Like

Hahaha. It’s so cool that you know a lot of these obsidian techy stuff, tho! Well, thank you once again for the help. And if you let me share with you, Jesus loves you! :wink: :blush:

1 Like

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