Sort tiddler titles by dates somewhere within the title

I have dates in tiddler titles in yyyy-mm-ddd format surrounded by other various text. The following filter returns the titles that have dates somwhere in them:-

{{{ [regexp[\d{4}-\d{2}-\d{2}\b]]}}}

How can I get the results sorted by the date?

Try this:

{{{ [regexp[\d{4}-\d{2}-\d{2}\b]]
  :sort:date[search-replace::regexp[.*(\d{4}-\d{2}-\d{2}\b).*],[$1]] }}}

Notes:

  • The first filter run finds the tiddler titles (as you’ve already done)
  • Then, the :sort:date[...] filter run sorts those titles using a search-replace filter to extract just the date portion of each title
  • Note the regexp uses .*(...).* so the date pattern is within parentheses. This defines a regexp expression group, which is then referenced in the second operand using $1

Also, for readability, I recommend using a variable to hold the regexp date pattern, like this:

<$let re=".*(\d{4}-\d{2}-\d{2}\b).*">
{{{ [regexp<re>] :sort:date[search-replace::regexp<re>,[$1]] }}}

enjoy,
-e

1 Like

Why isn’t my version working?

<$let dre="\d{4}-\d{2}-\d{2}\b" re=".*(<<dre>>).*">
{{{ [[regexp<re>]sortsub[regexp<dre>]] }}}

Apparently I can’t even produce valid filter syntax yet. I wish filter syntax error messages could be more verbose. :thinking:

The first problem is your definition of re="...", which sets a literal value that is not wikified, so the <<dre>> is not being replaced by it’s value. Use filtered transclusion syntax to construct the value of re.

Next, you have too many square brackets in your filter syntax.

Lastly, to sort by the date, you need to ignore the leading/trailing title text that surrounds the actual date string. You can use search-replace::regexp<dre>,[$1] to do this. But, unlike the :sort[] filter run prefix, you can’t directly use square brackets within the sortsub[…] operator, so you’ll need another variable to hold the substitution parameter.

Try this:

<$let dre="\d{4}-\d{2}-\d{2}\b" re={{{ [[.*(]] [<dre>] [[).*]] +[join[]] }}} sub="$1">
{{{ [regexp<re>sortsub:date[search-replace::regexp<dre>,<sub>] }}}
2 Likes

Thank you Eric that works well. I am thinking that it may be easier to have a user field called ‘date’ how would I do that?

I support this, I have often argued why have compound tiddler titles (or any field content), when you can use a field?, if you are having trouble making unique titles that has other options. In a way a compound value is a form of encoding and you need to decode it to use it.