Query Formatted Blocks and put in chronological order

I am using the following query and generated the result in the picture. So now I want to put the latest date at the top and in chronological order. Tried three transform result codes from the community. Doesn’t work. Wonder how I can make it happen.

ps. Most of the blocks are under a [[date]] block, but some are not. Not sure if this would cause some trouble.

#+BEGIN_QUERY
{:title [:h2 “Comment Blocks”]
:inputs [:current-page]
:query [:find (pull ?b [*])
:in $ ?cp
:where
[?p :block/name ?cp]
[?b :block/page ?p]
[?b :block/content ?c]
[(re-pattern “(^>)”) ?regex]
[(re-find ?regex ?c)]]
}
#+END_QUERY

Please be more specific with your requirements and examples:

  • There should be a consistent place to read the date from. Is this the parent block?
    • What codes have you tried? They most probably read the date from a different place.
  • Your date format doesn’t help. Is it a reference to the journal?
    • It is strange that you use Timeline instead of the journal itself.

So this is a page that I used to collect data for a specific purpose. I have a heading in that page called Timeline and other titles for other purpose. Under Timeline block, I have structured the notes by dates. In the page, I have something like “conclusions” here and there. By using the query, I called out all the conclusions and generate the result on top of each page so that I can quickly review them. Most of the conclusions are made under each Date. So when I queried the conclusions out, the breadcrumbs show the dates. I want to see the latest one on top of the query instead of the oldest.

This whole thing is not related to dates in journal. This is a page with Date blocks in it.
Below is an example.
image

If the dates are not related to the journal and they don’t follow a sortable format (i.e. year/month/date), there is no easy way to sort them. But they look like having a dedicated color. Is this purely decorative or is it a link to somewhere?

It is a link to the daily journal page.
ps. thanks for following up!

Something like this:

#+BEGIN_QUERY
{:title [:h2 "Comment Blocks"]
 :inputs [:current-page]
 :query [:find ?date (pull ?b [*])
   :keys date b
   :in $ ?cp
   :where
     [?p :block/name ?cp]
     [?b :block/page ?p]
     [?b :block/content ?c]
     [(re-pattern "(^>)") ?regex]
     [(re-find ?regex ?c)]
     [?b :block/parent ?parent]
     [?parent :block/refs ?journal]
     [?journal :block/journal-day ?date]
 ]
 :result-transform (fn [result] (map :b
   (sort-by (comp - :date) result)
 ) )
}
#+END_QUERY
1 Like

Hi Thank a lot. It worked. Now it is in the chronological order that I wanted.
Just one more problem here. There are a few blocks with the formatted text but are NOT under a journal date. Is there a way to call out those as well. It’s better that they can be put at the bottom of the search.

Replace these lines:

     [?parent :block/refs ?journal]
     [?journal :block/journal-day ?date]

…with these lines:

     (or-join [?parent ?date]
       (and
         [?parent :block/refs ?journal]
         [?journal :block/journal-day ?date]
       )
       (and
         (not [?parent :block/refs])
         [(+ 0 0) ?date]
       )
     )
1 Like

Thank you so much. It worked perfectly.

This is the final query that I’m using now.
It is used to search formatted blocks in a page and arranged by its parent block journal date.

#+BEGIN_QUERY
{:title [:h2 “:loudspeaker:Conclusion & Action”]
:inputs [:current-page]
:query [:find ?date (pull ?b [*])
:keys date b
:in $ ?cp
:where
[?p :block/name ?cp]
[?b :block/page ?p]
[?b :block/content ?c]
[(re-pattern “(^>)”) ?regex]
[(re-find ?regex ?c)]
[?b :block/parent ?parent]
(or-join [?parent ?date]
(and
[?parent :block/refs ?journal]
[?journal :block/journal-day ?date]
)
(and
(not [?parent :block/refs])
[(+ 0 0) ?date]
)
)
]
:result-transform (fn [result] (map :b
(sort-by (comp - :date) result)
) )
}
#+END_QUERY