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
    Jun 13 '15 at 2:36
  • $\begingroup$ Yes very similar! $\endgroup$
    – M.R.
    Jun 13 '15 at 2:59
  • $\begingroup$ @Mr.Wizard 2181? :) $\endgroup$
    – Kuba
    Jun 13 '15 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
    Jun 13 '15 at 10:27

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.
    Jun 16 '15 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
    Jun 16 '15 at 18:30
  • $\begingroup$ @M.R. Using "WholeCellGroupOpener" works but I don't know why :) $\endgroup$
    – Kuba
    Jun 16 '15 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
    Dec 22 '15 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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