Hi!

I tried to use `org-sort-entries` programmatically to keep some
heading sorted when capturing in it or refiling in it. But I saw that
sometimes a different heading was the one being sorted. After
debugging it a little, I realize that `org-sort-entries` works only if
the target heading is visible in the buffer. If it's not, it sorts its
top-most parent instead.

To reproduce, just evaluate the following form:

(progn
  (switch-to-buffer
   (find-file-noselect (make-temp-file "bug-" nil ".org")))
  (insert "#+STARTUP: overview
* Top level heading
** Heading I don't care about 2
** Heading I want to sort
*** Item 1
*** Item 3
*** Item 2
** Heading I don't care about 1
")
  (save-buffer)
  (org-mode)
  (goto-line 4)
  ;; (org-reveal)
  (org-sort-entries nil ?a))

You will see that the level 2 headings become sorted, when it's the
level 3 ones the ones that we wanted to sort. If you uncomment
`(org-reveal)` and evaluate again, you will see that then it works fine.

------------------------------------------------------------------------

Emacs  : GNU Emacs 30.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.41, 
cairo version 1.18.0)
 of 2025-12-31
Package: Org mode version 9.8 (release_9.8-1-g26ed78 @ 
/home/ignacio/repos/built-from-source/org-mode/lisp/)

Reply via email to