lawlist
lawlist

Reputation: 13457

How to custom sort tags using org-tags-sort-function with org-set-tags

I would like to use C-c C-q to set tags from within the org capture templates buffer, and have the :event: tag (if it is used) always appear first in time (i.e., to the left of any other tags). I set up an org capture template for events that inserts the initial tag of :event:. However, org-set-tags defaults to prepend new tags to the beginning of the list instead of append them to the end of the tag list.

I see an option within the source code of org-set-tags for using a custom org-tags-sort-function, but I was not able locate any examples from which to copy / modify. Does anyone have a sample of how to use org-tags-sort-function with org-set-tags so that something like the tag :event: could be automatically sorted first (i.e., to the left of all other tags)?

Upvotes: 1

Views: 499

Answers (1)

Nicolas Dudebout
Nicolas Dudebout

Reputation: 9262

A sort function is just a function comparing elements two at a time. There are two functions pre-defined in org.el in the defcustom of org-tags-sort-function:

(const :tag "Alphabetical" string<)
(const :tag "Reverse alphabetical" string>)

To build your custom sort function you can do the following:

(defun event-first (x y)
  (if (string= "event" x)
      t
    (if (string= "event" y)
      nil
      (string< x y))))

I chose, arbitrarily, to sort the non-"event" tags alphabetically but you can change that.

Upvotes: 1

Related Questions