[SOLVED] Sorting of results is disturbed

I have this query:

#+BEGIN_QUERY
{:title [:h3 "Not tell"]
 :query [:find (pull ?b [*])
  :where
   [?b :block/properties ?prop]
   [(get ?prop :type) ?type]
   [(= ?type "not-tell")]
 ]
:result-transform (fn [r] (sort r))
}
#+END_QUERY

It sorts the results for type:: not-tell based on the journal date (yyyy-mm-dd), with the most recent date at the top. That worked fine, until the latest entry, today, which gets put at the bottom of the list instead of at the top. All the other entries are sorted as expected.

Note: the property used to be topic, and I changed all of the blocks to type, and I also made that change in the query code. However, I can’t see anything in the query that would upset the sorting order, while the format of today’s block is the same as the other formats.

Try to make the result-transform explicit :slight_smile: it doesn’t sort the way you expect now. (it basically uses something vague like block id or something… idk)

:result-transform (fn [result] (sort-by (fn [r] (get-in r [:block/page :block/journal-day])) result)) ; sort the result by the journal day

1 Like

Makes sense. I added a new entry today and they are all mixed. Yesterday and today’s results aren’t even contiguous.

However, as usual, your suggestion works. Right now the oldest date is at the top of the list, the most recent one is at the bottom. Is it possible to have a reverse order?

Yes definitely! Adding in a simple >.
:result-transform (fn [result] (sort-by (fn [r] (get-in r [:block/page :block/journal-day])) > result)) ; sort the result by the journal day

1 Like

Jesus, woman, you ARE amazing!!
I have marked your 1st reply as the solution.
Thanks a lot for your help.

1 Like

Does this work in version 0.10.6? I’m not seeing any change in the sorting for this query:

  #+BEGIN_QUERY
  {
  :title [:b "Upcoming Events, next 10 days"]
  :query [:find (pull ?b [*])
         :in $ ?start ?next
         :where
         [?p :block/name "event"]
         [?b :block/refs ?p]
         (between ?b ?start ?next)
        ]
  :inputs [:today :+10d]
  :result-transform (fn [result] (sort-by (fn [r] (get-in r [:block/page :block/journal-day])) > result)) ; sort the result by the journal day
  :group-by-page? true
  :collapsed? false
  }
  #+END_QUERY

:group-by-page? true and :result-transform are incompatible with each other. Either you see blocks per page (default view) or you can do a custom sort with result-transform.
This would sort the blocks in whichever way you wish.

1 Like