Action to sort all headers (with blocks) by alphanumeric order?

For example:

# Bananas
Random facts about bananas: they’re yellow.
  
# Oranges
Objectively the best fruit.
 
# Apples
Granny smith are okay, the rest are terrible.

(Sorted by alphanumeric order would be)

# Apples
Granny smith are okay, the rest are terrible.

# Bananas
Random facts about bananas: they’re yellow.
  
# Oranges
Objectively the best fruit.

Anyone want to take a stab at it?

This would make a great first project to learn some basic coding.

In pseudo-code, you’d want to:

  1. Split the drafts into an array at each line starting with a #

  2. Sort the array

  3. Combine the array

  4. Set the contents of the draft to the newly combined array.

2 Likes

This is where an enhanced Block Mode (Line Mode?) could really shine.

In case you still need help, here’s something to get you started:

const blocks = draft.content.split('#');
draft.content = blocks.sort().join('#');

Note:
This might get weird if there is no empty line following last block, as the join doesn’t insert any line changes or blank lines. To fix that, you probably want to add code to strip all line changes at the end of each block, and then add required line changes (’/n’) with the join command.