Is there a way to programmatically reorder the section cells in a notebook alphabetically?

For example: enter image description here

  • $\begingroup$ I think a similar question has been asked before, possibly relating to cells that are labeled by date, but I cannot find it now. Edit: never mind, I seem to have been thinking of your own question: (73544) $\endgroup$
    – Mr.Wizard
    Commented Jun 13, 2015 at 2:36
  • $\begingroup$ Yes very similar! $\endgroup$
    – M.R.
    Commented Jun 13, 2015 at 2:59
  • $\begingroup$ @Mr.Wizard 2181? :) $\endgroup$
    – Kuba
    Commented Jun 13, 2015 at 5:38
  • $\begingroup$ My answer should work for you but it would be nice to have more details described. What if those sections are gouped by Titles for example. Do you want to be able to do this only on selected part too. etc etc. I think a lot of such features may be implemented, I just don't like to think what others may need for edge cases. $\endgroup$
    – Kuba
    Commented Jun 13, 2015 at 10:27

1 Answer 1


I will try to add a more general function later, meanwhile you can use this procedure:

Module[{cells, data},

 cells = Cells[CellStyle -> "Section"];

 data = (
     SelectionMove[#, All, CellGroup, AutoScroll -> False];
     ) & /@ SortBy[cells, First@NotebookRead[#] &];

  SelectionMove[cells[[i]], All, CellGroup, AutoScroll -> False];
  NotebookWrite[EvaluationNotebook[], data[[i]], AutoScroll -> False]
  {i, Length@cells}
  • $\begingroup$ Oh no, I didn't realize this solution would destroy cell options like Cell["Python", "Section", WholeCellGroupOpener->True] $\endgroup$
    – M.R.
    Commented Jun 16, 2015 at 17:30
  • $\begingroup$ @M.R. well that's unexpected. Any other options behave strange? I've posted the question already: mathematica.stackexchange.com/q/86077/5478 $\endgroup$
    – Kuba
    Commented Jun 16, 2015 at 18:30
  • $\begingroup$ @M.R. Using "WholeCellGroupOpener" works but I don't know why :) $\endgroup$
    – Kuba
    Commented Jun 16, 2015 at 21:33
  • $\begingroup$ @M.R. I've post a solution to the problem of CellContext: 102269 so you can use this replacement rules. $\endgroup$
    – Kuba
    Commented Dec 22, 2015 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.