Sorting a query by block order on page?

I have a query that simply returns all the blocks on the current page:

#+BEGIN_QUERY
{:query
 [:find (pull ?b [*])
 :in $ ?current-page
 :where
 [?p :block/name ?current-page]
 [?b :block/page ?p]
 ]
 :inputs [:current-page]
}
#+END_QUERY

Is it possible to sort the result by the order that the blocks appear on the page? Created-time is a decent proxy but is not ideal.

3 Likes

@rob this is a great question I really wish to know the answer to as well.
I posted it also on discord and have an ongoing discussion there. I am hoping something will come out of it.

1 Like

I also created the following GitHub issue: Results of queries shows blocks in page out of order · Issue #7833 · logseq/logseq · GitHub

1 Like

hi,

this is also a concern for me, and there is a workaround on this, may anyone get a better solution …

so far, the workaround is:

number the block, e.g. : b 1, b 2, b 2.1, b 3 …

image

and … query logseq in the same page:

#+BEGIN_QUERY
{:title [:h3 "query for logseq in this page"]
:query (and (page <% current page %>) "logseq")
:result-transform (fn [result] 
(sort-by 
    (fn [d] (get d :block/content ))
    result))
}
#+END_QUERY

the result keeping same order as on the page

image

also tried to toggle bullet into numbered list, but not working with "d :block/content " in the code,

thank you for any better idea.