问题
i have lines that i want to sort from the highest number to the lowest or the opposite so not really an issue
exemple from this:
steven:class3 | Pounds: 6
ibesom:class1 | Pounds: 125
heller:class1 | Pounds: 13
to this:
ibesom:class1 | Pounds: 125
heller:class1 | Pounds: 13
steven:class3 | Pounds: 6
Thank you guys
回答1:
For simple sorting tasks you can just rearrange the contents of the line, sort, then return the lines to their original form. This can often be done with simple regular expressions. See this question for an example.
For more complex cases, perhaps involving two or more items, the items to be sorted can be collected from the input line using a regular expression and then inserted at the start of the line. It is useful to add a separator between the sort terms and the original line. After the sort is done the sort terms and the separator are removed.
For this question a simple rearrangement then sort and then rearrange back would suffice. But, using the more general style is instructive.
Firstly, choose a separator. Use a character or a string that does not occur within the text to be sorted. For this I choose ;'#
.
Next create a regular expression find and replace to extract the search terms and build the line to be sorted. For this the find-what is ^(.*)\b(\d+)$
and the replace-with is $2 ;'#$0
.
This changes the example input into:
6 ;'#steven:class3 | Pounds: 6
125 ;'#ibesom:class1 | Pounds: 125
13 ;'#heller:class1 | Pounds: 13
Now use menu => Edit => Line operations => Sort as integers ascending. That yields:
6 ;'#steven:class3 | Pounds: 6
13 ;'#heller:class1 | Pounds: 13
125 ;'#ibesom:class1 | Pounds: 125
The method used is not limited to integers. The sort term can be built using any characters and the appropriate sort used.
The final step is to remove the sort term and the separator. Use a regular expression replace-all finding ^.*;'#
and replace with nothing.
The only challenge is creating the first regular expression to generate the sort term and add it to the line. The general plan is that the find-what matches the whole of the input line, so it starts with ^
and ends with $
. Then the replace-with has the three items "sort term", "separator" and $0
. The final $0
includes the whole of the original input line.
来源:https://stackoverflow.com/questions/60613655/sort-lines-after-a-certain-character-in-notepad