I would like to sort paragraphs in proper numerical order. The paragraphs all begin with the same string, but are followed by different numbers, for example:
Nisus 1: Writer
Nisus 10: Writer
Nisus 11: Writer
Nisus 12: Writer
Nisus 2: Writer
Nisus 23: Writer
Nisus 4: Writer
What I would like to have is this:
Nisus 1: Writer
Nisus 2: Writer
Nisus 4: Writer
Nisus 10: Writer
Nisus 11: Writer
Nisus 12: Writer
Nisus 23: Writer
How to sort paragraphs in numerical order when the numbers are preceded with a text string?
Re: How to sort paragraphs in numerical order when the numbers are preceded with a text string?
G'day, Þorvarður et al
One idea is to transfer the numeric strings to the beginning of each paragraph, perform the sort, then restore the numeric strings to their original positions.
Another approach is to add leading zeros to the numeric strings as necessary, so that all of them are (in this instance) 2-digit strings. Perform the sort, then remove the leading zeros. This is not as simple as the above method, but it's worth bearing in mind as it may be useful in other situations.
Cheers,
Adrian
One idea is to transfer the numeric strings to the beginning of each paragraph, perform the sort, then restore the numeric strings to their original positions.
Code: Select all
Find and Replace @Text<^(.+) (\d+):>, @Text<\2 \1:>, 'Esa'
Sort Ascending (A-Z)
Find and Replace @Text<^(\d+) (.+):>, @Text<\2 \1:>, 'Esa'
Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
macOS Ventura
Nisus Writer user since 1996
Re: How to sort paragraphs in numerical order when the numbers are preceded with a text string?
Here is my solution to this problem.
Use the following macro on a simple selection. It should sort all paragraphs in the usual manner (ASCII sort) and if any of the paragraphs start the same and have numbers, then the numbers will determine the sort, and you won't need to zero pad stuff either.
So
Use the following macro on a simple selection. It should sort all paragraphs in the usual manner (ASCII sort) and if any of the paragraphs start the same and have numbers, then the numbers will determine the sort, and you won't need to zero pad stuff either.
So
- Nisus 12
- Apple
- Software
- Writer 52
- Nisus 3
- Writer 6
- Apple
- Nisus 3
- Nisus 12
- Software
- Writer 6
- Writer 52
- Attachments
-
Sort Paragraphs Numerically with Prefix.nwm
- (6.63 KiB) Downloaded 23 times
philip
Re: How to sort paragraphs in numerical order when the numbers are preceded with a text string?
Thank you Philip for this elaborate macro!
Last edited by Þorvarður on 2025-04-19 11:50:59, edited 2 times in total.
Re: How to sort paragraphs in numerical order when the numbers are preceded with a text string?
Hello Adrian,
Thank you very much for you solution.
My own solution was originally to (1) cut the string, (2) sort, and then (3) put the string back in again. Your Find and Replace approach is much better and more straight forward, of course. Moreover, in der Kürze liegt die Würze. :–)
I include mine here below. It gets the job done although it doesn't look particularly elegant.
Thank you very much for you solution.
My own solution was originally to (1) cut the string, (2) sort, and then (3) put the string back in again. Your Find and Replace approach is much better and more straight forward, of course. Moreover, in der Kürze liegt die Würze. :–)
I include mine here below. It gets the job done although it doesn't look particularly elegant.
Code: Select all
# Ensure something is selected.
$textExpression = Read Selection
If $textExpression == ''
$proceed = Prompt "You need to select some text first. Click now 'OK' and then select the text.", '', 'OK, I will.'
# The following sleep-trick is from Adrian
While $textExpression == ''
Sleep 1
$textExpression = Read Selection
End
End
$sort_ignoring = Prompt Input 'Enter the string that should be ignored when sorting.', 'Case matters!', 'OK', 'Nisus'
$counter = Find All "(?:^.+\$)", 'Esa'
Find and Replace "$sort_ignoring", "", 'eaS'
Edit:Transform Paragraphs:Sort Ascending (A-Z)
Select Start
Type Text $sort_ignoring
While $counter >0
Find "^", 'E'
Type Text $sort_ignoring
$counter = $counter-1
End
Last edited by Þorvarður on 2025-04-19 11:52:14, edited 1 time in total.
Re: How to sort paragraphs in numerical order when the numbers are preceded with a text string?
G'day, Þorvarður et al
I suspect the reason the final item in the list goes missing when using Philip's Macro is that you haven't included a final paragraph return in your selection.
Cheers,
Adrian
I suspect the reason the final item in the list goes missing when using Philip's Macro is that you haven't included a final paragraph return in your selection.
Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
macOS Ventura
Nisus Writer user since 1996
Re: How to sort paragraphs in numerical order when the numbers are preceded with a text string?
Thanks, Adrian, for pointing that out. When I posted my macro I was thinking, I bet someone is going to have that problem.
Anyhow, the .sortWithCommand is just such a cool feature. But it only works on arrays. So you have to first put all the things you want to sort into an array. If you want to sort paragraphs, you have two options; either split the selection on the returns and then add them back in at the end. Or otherwise include the returns with the paragraphs. In this macro, I did the latter, which means you need to make sure the last paragraph has a return at the end. I should have added a check to add a return at the end if necessary.
philip
Re: How to sort paragraphs in numerical order when the numbers are preceded with a text string?
So here is a version of the macro with the check added.
- Attachments
-
Sort Paragraphs Numerically with Prefix.nwm
- (7.31 KiB) Downloaded 18 times
philip