Advanced query not sorting resulting pages alphabetically

Hi all, I have a number of pages which have a name as the Page Name (for example [[A Person]]), and for each page there is a property called company, which specifies the company they work for, e.g. company:: "ACME". I am trying to create a query to list all team members for a given company alphabetically. The following query returns all the team members but they seem to be listed according to the Journal entry date rather than alphabetically. Am I missing something obvious here?

[[A person]]
company:: ACME
[[Another person]]
company:: ABC Ltd
#+BEGIN_QUERY
{
:title "Team Listing"
:query [
:find (pull ?p [*])
:where
[?p :block/properties ?prop]
[(get ?prop :company) ?company]
[(= ?company #{"ACME"})]
]
:table-view? true
 :result-transform (fn [result] 
  (sort-by (fn [r] (get-in r [:block/page :block/name])) result))
}
#+END_QUERY
  • This line:
    (sort-by (fn [r] (get-in r [:block/page :block/name])) result))
    
    sorts according to the name of the page that each found block (r) is in. If they are in the journal, then they are sorted according to the journal name (which may not be alphabet-aware itself).
  • But even the rest of your query doesn’t follow your description, even though it happens to return the team members. More specifically, it is not clear:
    • whether the value of property company:: is ACME or [[ACME]]
    • whether the property company:: of each person is assigned:
      • in that person’s page-properties
        • per “for each page there is a property called company”
      • in a block of some random journal entry
        • per “the Journal entry date”
  • Going by your current query, if you want to return (sorted) the team members, should probably do this:
    #+BEGIN_QUERY
    {:title "Team Listing"
     :query [:find (pull ?ref [*])
       :where
         [?b :block/properties-text-values ?prop]
         [(get ?prop :company) ?company]
         [(str "[[" "ACME" "]]") ?company-link]
         [(= ?company ?company-link)]
         [?b :block/refs ?ref]
         [?ref :block/original-name ?ref-name]
         [(!= ?ref-name "company")]
         (not
           [(str "[[" ?ref-name "]]") ?ref-link]
           [(= ?ref-link ?company)]
         )
     ]
     :table-view? true
     :result-transform (fn [result] (sort-by :block/original-name result))
    }
    #+END_QUERY
    

Hi @mentaloid - many thanks for your help and apologies if my initial post was a little garbled. I’m still finding my way with Logseq. Essentially what I want to set up is a Page for a company (ACME) and a page for each Employee. On the Employee page, the “company::” tag will link to the ACME page. To answer your questions:

  • the value of the property company:: is [[ACME]]
  • each person’s page will have a proper called company::

I hope this makes sense - and thanks for your patience!

If journal is not involved, then looks like all you have to do is to:

  • replace this line:
    (sort-by (fn [r] (get-in r [:block/page :block/name])) result))
    
  • with this one:
    (sort-by (fn [r] (get-in r [:block/name])) result))
    
  • or any variation of it, like these ones:
    (sort-by (fn [r] (get r :block/name)) result))
    (sort-by (fn [r] (:block/name r)) result))
    (sort-by :block/name result))
    

Thanks again @mentaloid . I’m not sure that this works exactly how I want as it appears to sort based on the block (i.e. company:: [[ACME]]) is I use [:block/name] to sort, rather than on the Page name (Wile Coyote, Roadrunner etc):

  • I have tested both queries and they work as intended. Here is the last one:
  • Your second screenshot lists blocks, while it should list pages.
    • In your query you are using ?p (for page), but apparently your properties are in blocks instead. Therefore, either:
      • Your page-properties got messed-up.
        • Check the respective markdown files in an external editor.
      • You still describe something different than you intend to.
        • Maybe you haven’t yet grasped the exact differences between some of the terms block, common page, journal page, block-property, page-property.
          • Granted that they are not intuitive and they also differ from other software.

Thanks for all the pointers @mentaloid, it’s a massive help to me. I’ve dug further into this based on your last feedback and it appears that I was missing “title:: Pagename” in the first block of each page. I had simply added “company:: [[ACME]]” and so even though it was the first block, it appears that it was treating it as a block property, not a page property. As soon as I added the ‘title’ property as well, the output comes out in alphabetical order as expected.

To me it works without property title::

  • That property is optional anyway.
  • Now that it works, chances are that removing property title:: won’t break it.
    • The important thing is to persuade the system that the first bullet:
      • is not a block
      • is frontmatter
1 Like