Sorting and mapping results in :result-transform

Based on the answers in this thread I’ve tried to add a sort to my “near future” query, so it sorts first on deadline, then scheduled.

   :result-transform (fn [result]
      (sort-by 
         (juxt (fn [r] (get r :block/deadline 99999999) ) (fn [r] (get r :block/scheduled 99999999) ) ) result
      )
      (->> result
         (map (fn [r] (assoc r :block/properties {
             "scheduled" (get r :block/scheduled)
             "deadline" (get r :block/deadline)
             "marker" (get r :block/marker)
          })))
      )
   )

Unfortunately, it just doesn’t seem to be doing anything. The sort is out of expected sequence

image

If I remove the mapping to include the extra fields in the results, the sorting works as expected, so that is fine. I’ve now put the sort logic in a number of places though, and either it doesn’t activate, or it produces an invalid query.

What is the correct :result-transform setup to achieve this?

Looks like a wrong syntax (although I’m not familiar with ->>).
UPDATE: ->> is Clojure’s thread-last macro
Should nest map inside sort-by, as if it was its result input (otherwise sort-by has no effect). Here is a recent question with a similar structure: Unite different :result-transform’s

I’m also not familiar with this syntax.
I never got it to work either.

I’m not actually sure where I got that ->>from now - pretty sure it was a query from another issue on here. Anyway, that does work because the properties show in the table OK, it’s just the combo I was having problems with.

Thanks for the link to that other thread - I think it’s sorted now. I took out the ->> part, though it works with or without it, which I find odd (i.e. it’s not a syntax error, but seems to make no difference … wish I could find the original suggestion I got that from now!)

So, for future reference, and to aid future seekers of knowledge, this is the final result-transform, which does what I want :slight_smile:

   :result-transform (fn [result]
     (sort-by 
         (juxt 
            (fn [r] (get r :block/deadline 99999999)) 
            (fn [r] (get r :block/scheduled 99999999)) 
          )

            (map 
               (fn [r] (assoc r :block/properties {
                   "scheduled" (get r :block/scheduled)
                   "deadline" (get r :block/deadline)
                   "marker" (get r :block/marker)
                   })
               ) result)
         
      )
   )
2 Likes

Probably not helpful anymore but ->> is actually a threading-macro: Clojure - Threading Macros Guide

I think it does nothing if there is only one transformation, the (map ... in your case, but there is no need to use it in that case anyway.

2 Likes