Search code examples
notepad++notepademeditor

Sorting a text file by the second value


I was wondering if there is an option in notepad++ to sort a text file by the second value.

I have a txt that looks like this :

('sinon', 143)
('serais', 113)
('sens', 107)
('se', 323)
('sans', 113)
('sais', 702)
('sa', 137)
('s', 382)
('rien', 619)
('quoi', 611)

I tried to sort it by the value of the number and not alphabetically with notepad but no sucess so far. I also tried a bit of python but as I'm not really good at coding I didn't suceed here.


Solution

  • Notepad++ can easily do the sorting. The method is to modify each line before doing the sort and then modify the lines again afterwards.

    For this case just move everything before the number to the end of the line. Do a regular expression replace all changing ^(.*, )(\d.*)$ to \2\1. This changes the input text to be

    143)('sinon', 
    113)('serais', 
    107)('sens', 
    323)('se', 
    113)('sans', 
    702)('sais', 
    137)('sa', 
    382)('s', 
    619)('rien', 
    611)('quoi', 
    

    Then do the sort using menu => Edit => Line operations => Sort lines as integers ascending. Finally rewrite the lines in their original form. Do a regular expression replace all changing $^(.*\))(.*) to \2\1.