Markdown headings sorting

I’m looking for an option to sort markdown headings for rearranginc the TOC. For example if I have

# 1
## c
## a
## b

# 3
## e
## f
## d

# 2
## h
## g
## i

how can I sort alphabetically only H1 headers by name? Or only the H2 headers? Or reverse sort?
Apparently Microsoft Word is able to do this with macros, but I don’t use it and I never used macros so I don’t know if it’s even doable in LO. Is this something that can be achieved in LibreOffice?

What is the question? Have you a MarkDown’ed text document you want to import and format in Writer?

If you feel brave, develop an input filter to convert MarkDown to LO styling system.


What is the purpose of your alphabetical sort? Do you want to reorder the text or simply create an alphabetical table of headings? The latter can be done with an Alphabetical Index. It can keep the level relationship up to 3 levels.
OS name, LO version and save format

I’m on Linux, LO 24.2.7.2, file.md

In markdown editors the TOC is automatically generated from the headings. Let’s say you have a big list of appointments, or to-do, or journal, anything where the heading is a date for example, like

# 2025-02-01
- a
- b
- c
# 2025-02-02
## 1
- a
- b
## 2
- c
- d

etc. this list might be very long, but I wnat to reverse sort it, I want the latest date on top and the oldest at the bottom. But the H2 and the text should not be sorted, only H1. Result should be

# 2025-02-02
## 1
- a
- b
## 2
- c
- d
# 2025-02-01
- a
- b
- c

Is this achievable?

I must go now, so have not enough time to experiment and answer. Have a try with the Sort feature and see how it interacts with the style system.

I tried that and it considers all the lines, I didn’t find a way to make it consider whole blocks of text

If your # n headings are styled as Heading n, you can sort them “by hand” (Ctrl+Up Arrow/Down Arrow) in the navigator.
image

1 Like

I tried in Outline Folding mode but then Sort is greyed out. Might be worth an enhancement Bug report?

.
Either that or a dialogue like Word, a quick search found this page, Sorting by Headings (Microsoft Word) but I notice that it would require outline folding anyway.