Query tasks by scheduled date *and* scheduled time

Hi,
I’d like to be able to view my tasks sorted by time as well as date. Currently, I have this query:

#+BEGIN_QUERY
{:title [:h3 "habits"]
:query [:find (pull ?h [*])
:in $ ?start ?today
:where
     [?h :block/marker ?m]
     (page-ref ?h "habit")
     [(contains? #{"TODO" "DOING"} ?m)]
     [?h :block/scheduled ?d]
     [(> ?d ?start)]
      [(< ?d ?today)]]
:inputs [:2d-before :tomorrow]
:result-transform (fn [result]
     (sort-by (fn [d] (get-in d [:block/scheduled])) result))
:breadcrumb-show? false
:table-view? false
}
#+END_QUERY

That produces the following:
image

In the above screenshot, “pm routine” is listed before “eat breakfast”, even though “eat breakfast” is scheduled at an earlier time. As far as I can tell, this is not an error with the pages or the sorting of the query. If I schedule “eat breakfast” on the next day, then the query orders the TODOs chronologically:

image

But when I have two or more tasks due on the same day, but at different times, they seem to be sorted by page title instead of time. Since “am routine” and “pm routine” were on the same page in the above screenshot, they are listed together. If I move “pm routine” to a different page, it is listed separately from “am routine” but still not chronologically:

image

I’d like to be able to see a more accurate representation of my day- down to the last minute I schedule a task. Ideally, tasks scheduled on the same day would also be sorted by time.

Please let me know if this can be solved by plugin or an update to my query.

Thank you!

Hi! I’m also facing the same problem. Have you figured out a solution/workaround by any chance?

Unfortunately I have not been able to sort with the Scheduled time. My current workaround is to forgo using the time section, and put a time stamp in the title of the task like:

  • TODO (13:00) thing at 1pm
    SCHEDULED: <2023-01-20 Fri>

Because “(“ come before regular characters when ordered alphabetically, all of my timed tasks are listed in order at the top of my query.

1 Like

Oh brilliant! How do you order alphabetically on top of sorting by date? Does it do that by default? Thanks

I currently use this query for the “today” section of my agenda:

#+BEGIN_QUERY
{:query [:find (pull ?b [*])
:in $ ?start ?next 
:where
    [?b :block/marker ?m]
    [(contains? #{"TODO" "DOING"} ?m)]
    (or [?b :block/scheduled ?d] [?b :block/deadline ?d])
    [(>= ?d ?start)]
      [(<= ?d ?next)]]
:inputs [:30d-before :today]
:result-transform (fn [result]
    (sort-by (fn [h] (get-in h [:block/content])) result))
:breadcrumb-show? false
:table-view? false
}
#+END_QUERY

And it does sort alphabetically due to the result transform at the bottom.

1 Like