Sorting by journal-day and hiding the page name in advanced queries

I have created blocks in the journal that references certain pages on different days.

I would like to write a query that finds all blocks that references a certain page and show only the children blocks. They should be sorted in chronological/ascending order of the journal’s day. If possible I would like to hide the page name/journal date and show only the blocks.

I succeeded in obtaining all the children blocks but failed to sort them correctly and cannot find a way to hide the page name/journal date. My current query:

#+BEGIN_QUERY
{
  :query [
    :find (pull ?b [*])
    :where
    [?p :block/name "my page name"]
    [?x :block/refs ?p]
    [?b :block/parent ?x]
  ]
  :result-transform (fn [result]
                      (sort-by (fn [h]
                                 (get (get h :block/page) :block/journal-day))
                               result))
  :breadcrumb-show? false
  :group-by-page? false
}
#+END_QUERY
1 Like

Welcome. Try this:

#+BEGIN_QUERY
{
  :query [
    :find ?date (pull ?b [*])
    :keys date b
    :where
      [?p :block/name "my page name"]
      [?x :block/refs ?p]
      [?b :block/parent ?x]
      [?x :block/page ?x-p]
      [?x-p :block/journal-day ?date]
  ]
  :result-transform (fn [result]
    (for
      [
        res
        (sort-by
          (fn [r] (get r :date))
          result
        )
      ]
      (get res :b)
    )
  )
  :breadcrumb-show? false
  :group-by-page? false
}
#+END_QUERY
1 Like

Thank you, your solution works.

If it is possible can you explain why does my original query fail to sort the blocks according to the :block/journal-day?

Because (get h :block/page) returns a number, not an object with field :block/journal-day etc.

1 Like

Instead of

It should be (get-in h [:block/page :block/journal-day])

Or the way @mentaloid did it.

I have tried replacing it with (get-in h [:block/page :block/journal-day]) but it still doesn’t return blocks in the correct order (ascending/chronological).

I think the get-in will not work in this case because the :block/journal-day is not nested under :block/page. :block/page is in a another object representing the journal entry for a specific date. Other than there is a way to query and get the object of the page ID, we can’t get to page’s :block/journal-day entry within :result-transform

Edited: Seems like the :block/journal-day is nested under :block/page. Curious why this doesn’t work? OK it actually works :tada:. I was using an older version of LogSeq (0.9.X), but the new v0.10.5 works fine. Thanks!

Thanks for the suggestion. It certain helped me better understand Clojure :grinning:

1 Like

It worked for a few days, now it is back to showing the pages name and sorted in reverse chronological order. Not sure what happened :anguished:

It seems to work on v0.10.5 but not in v0.9.X. I was trying it out on my other PC with a newer version which works fine and when I tried it on my current PC with the older version it fails. Thanks for all your help! :smiley: