Sorting text with numbers
-
I tried using the line operations tool but it doesn’t seem to be working. So having a test that is:
Warsongs rolls 206 Tykee rolls 206 Shadowhunter rolls 798 Ravencauthon rolls 229 Omgwtflolroflgg rolls 479 Nephlite rolls 576 Groverton rolls 943 Blodhgram rolls 829 Azria rolls 675 Antonioklaus rolls 720
I would like to sort that based on the numbers so that it shows me:
Groverton rolls 943! Blodhgram rolls 829! Shadowhunter rolls 798! Antonioklaus rolls 720! Azria rolls 675! Nephlite rolls 576! Omgwtflolroflgg rolls 479! Ravencauthon rolls 229! Tykee rolls 206! Warsongs rolls 206!
No matter what I try, It’s not working.
-
As you’ve seen, Notepad++’s Edit | Line Operations | Sort Lines As Integers and Sort Lines As Decimals operations look from the left of the lines and stop as soon as they find something that isn’t a number; so you can’t use them on the data as is.
There are a couple ways around this.
One way is to use regular expressions to transform the file so the numbers are at the beginning of each line; then sort; then reverse the transformation. That way doesn’t require anything but built-in Notepad++ functions… someone will probably come along to elaborate.
Another way is to use the Columns++ plugin. You can find it in Plugins | Plugins Admin…; save any open files first, then check the box beside Columns++ and click the Install button near the top right of the dialog.
The Sort Descending (numeric) action from the Columns++ menu works on your example — it looks for a number in each line and uses that number for sorting.
It does one thing different from your example: since the lines beginning
Warsongs
andTykee
have the same number, it leaves them in the original order.For trickier problems you can use the custom Sort… action on the Columns++ menu to specify a regular expression which defines exactly what you want to sort. See the help for details — it’s powerful, but a bit complicated.
-
@Michael-Gomez
As an alternative, the MultiReplace plugin offers a different approach. It has a dedicated dialog where, for your example, you sort in two steps: first, you add column 1 and sort ascending, and then you change the column to 3 and sort descending.The function handles mixed numbers and text automatically. The first row is marked by default as a header, so it won’t be sorted; this is configurable.
-
Hello, @michael-gomez, @coises, @thomas-knoefel and All,
As @coises said, you could achieve your goal with the buit-in regular expression engine of Notpead++ !
So, from your INPUT text :
Warsongs rolls 206 Tykee rolls 206 Shadowhunter rolls 798 Ravencauthon rolls 229 Omgwtflolroflgg rolls 479 Nephlite rolls 576 Groverton rolls 943 Blodhgram rolls 829 Azria rolls 675 Antonioklaus rolls 720
Using the following regex S/R :
-
FIND
(?-s)^.+?(\d+)
-
REPLACE
\1 $0!
=> You should get this temporary text :
206 Warsongs rolls 206! 206 Tykee rolls 206! 798 Shadowhunter rolls 798! 229 Ravencauthon rolls 229! 479 Omgwtflolroflgg rolls 479! 576 Nephlite rolls 576! 943 Groverton rolls 943! 829 Blodhgram rolls 829! 675 Azria rolls 675! 720 Antonioklaus rolls 720!
Then using the menu command :
Edit > Line operations > Sort Lines Lexicographically Descending
( Should be better sorted than using theSort Lines As Integers Descending
! )You would be left with that temporary text :
943 Groverton rolls 943! 829 Blodhgram rolls 829! 798 Shadowhunter rolls 798! 720 Antonioklaus rolls 720! 675 Azria rolls 675! 576 Nephlite rolls 576! 479 Omgwtflolroflgg rolls 479! 229 Ravencauthon rolls 229! 206 Warsongs rolls 206! 206 Tykee rolls 206!
And, finally, with the last regex S/R, below :
-
FIND
^\d+\x20
-
REPLACE
Leave EMPTY
Here is you expected OUTPUT text :
Groverton rolls 943! Blodhgram rolls 829! Shadowhunter rolls 798! Antonioklaus rolls 720! Azria rolls 675! Nephlite rolls 576! Omgwtflolroflgg rolls 479! Ravencauthon rolls 229! Warsongs rolls 206! Tykee rolls 206!
But, indeed, as @coises mentionned it, the two records with the same integer part
206
are inverted !Best Regards,
guy038
-