Sorting a text file by the second value

淺唱寂寞╮ 提交于 2020-05-24 05:17:29

问题


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.


回答1:


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.




回答2:


I am the developer of EmEditor. If you EmEditor, you can switch to the CSV mode, select the second column, and select the "Sort Smallest to Largest" command. Sorting by column in EmEditor



来源:https://stackoverflow.com/questions/59987307/sorting-a-text-file-by-the-second-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!