Hi all,
I need to sort the result of my advance query per created time in descending order.
Any help?
In general, I would like to have more info on how to structure in the advanced query the sorting.
Thanks in advance
Hi all,
I need to sort the result of my advance query per created time in descending order.
Any help?
In general, I would like to have more info on how to structure in the advanced query the sorting.
Thanks in advance
For sorting we use the clojure function sort-by in the result-transform clause.
See here for details on the sort-by function:
https://clojuredocs.org/clojure.core/sort-by
Then for the query it will be like this:
:result-transform (fn [result]
(sort-by
(fn [d] (get d :block/created-at)) ; this is the keyfn from the docs
result ; this is the coll from the docs
)
)
Within the get
we can select any attribute the query gives us.
We can add a >
before result
to reverse the sort order.
Hope this helps!
Thanks I’ll try.
One question if I want to sort the results by alphabetical order, can I use get block/name?
Or there is a better way?
Yes definitely! As a bonus it will sort case insensitive. If you want a case sensitive sort you can use block/original-name
It seems not working for me
the code is
#+BEGIN_QUERY
{:title
[:h4 {:class "font-bold px-1" :style {:color "#2b2b2a":background-color "#9eb8f0"}}
"📓 Today's Journal"]
:query [:find (pull ?b [*])
:in $ ?page
:where
[?r :block/name "journal"]
[?b :block/refs ?r]
[?p :block/name ?page]
[?b :block/page ?p]]
:inputs [:current-page]
:table-view? false
:result-transform (fn [result]
(sort-by
(fn [d] (get d :block/created-at))
> result))
:breadcrumb-show? false
:collapsed? true}
}
#+END_QUERY
I add the > and it should show in the reverse order, correct?
What did I do wrong?
thanks
Also with the original-name seems not to work
the code being
#+BEGIN_QUERY
{:title
[:h4 {:class "font-bold px-1" :style {:color "#2b2b2a":background-color "#9eb8f0"}}
"📓 Today's Journal"]
:query [:find (pull ?b [*])
:in $ ?page
:where
[?r :block/name "journal"]
[?b :block/refs ?r]
[?p :block/name ?page]
[?b :block/page ?p]]
:inputs [:current-page]
:table-view? false
:result-transform (fn [result]
(sort-by
(fn [d] (get d :block/original-name))
result))
:breadcrumb-show? false}
#+END_QUERY
I must do something wrong
Thanks
You’re getting blocks with your query. block/name and block/original-name are for pages.
block/created-at is only available for blocks if the feature is turned on in config.
For sorting by the content of the block you can use block/content.
Your query code is fine as far as I can tell.
Set this to true in the config file: :feature/enable-block-timestamps? false