Skip to content

Instantly share code, notes, and snippets.

@BasicAcid
Created November 1, 2025 10:52
Show Gist options
  • Select an option

  • Save BasicAcid/77c341ec691b830edf0bb369a0a7ad2f to your computer and use it in GitHub Desktop.

Select an option

Save BasicAcid/77c341ec691b830edf0bb369a0a7ad2f to your computer and use it in GitHub Desktop.
Sort org list items: unchecked first (no priority, then by priority), then checked.
(defun mf/org-sort-list-by-checkbox-and-priority ()
"Sort org list items: unchecked first (no priority, then by priority), then checked."
(interactive)
(org-sort-list
nil ?f
(lambda ()
;; Extract the sorting key for each item
(let* ((item (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(checked (string-match "\\[X\\]" item))
(priority (when (string-match "\\[#\\([A-C]\\)\\]" item)
(match-string 1 item))))
;; Return a sort key: [checked-status, has-priority, priority-letter]
;; Lower numbers sort first
(format "%d-%d-%s"
(if checked 1 0) ; unchecked (0) before checked (1)
(if priority 1 0) ; no priority (0) before priority (1)
(or priority "A")))) ; sort by priority letter
#'string<))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment