0

Here are some sample strings, sorted by longest string to shortest sting:

"Bi(NO<sub>3</sub>)<sub>3</sub>·5H<sub>2</sub>O","bismuth nitrate pentahydrate"); 
"Be(C<sub>5</sub>H<sub>7</sub>O<sub>2</sub>)<sub>2</sub>","Be acetylacetonate"); 
"O<sub>2</sub><sup>2&minus;</sup>","Peroxide#Inorganic chemistry|peroxide ion"); 
"Cu(BrO<sub>3</sub>)<sub>2</sub>·6H<sub>2</sub>O","Cu bromate hexahydrate");
"Mn(CHO<sub>2</sub>)<sub>2</sub>·2H<sub>2</sub>O","Mn formate dihydrate"); 

If we use "," as a delimiter, the substrings would be this:

"Bi(NO<sub>3</sub>)<sub>3</sub>·5H<sub>2</sub>O
"Be(C<sub>5</sub>H<sub>7</sub>O<sub>2</sub>)<sub>2</sub>
"O<sub>2</sub><sup>2&minus;</sup>
"Cu(BrO<sub>3</sub>)<sub>2</sub>·6H<sub>2</sub>O
"Mn(CHO<sub>2</sub>)<sub>2</sub>·2H<sub>2</sub>O

I want to sort the substrings by longest string to shortest sting. How can I do this?

1 Answer 1

2

You could use external commands to do that. Take for example, giving that the buffer has only the content you share the following commands:

%!awk -F ',' '{print length($1) "|" $0}'
%!sort -nr
%norm df|

would result in this contents:

"Be(C<sub>5</sub>H<sub>7</sub>O<sub>2</sub>)<sub>2</sub>","Be acetylacetonate"); 
"Mn(CHO<sub>2</sub>)<sub>2</sub>·2H<sub>2</sub>O","Mn formate dihydrate"); 
"Cu(BrO<sub>3</sub>)<sub>2</sub>·6H<sub>2</sub>O","Cu bromate hexahydrate");
"Bi(NO<sub>3</sub>)<sub>3</sub>·5H<sub>2</sub>O","bismuth nitrate pentahydrate"); 
"O<sub>2</sub><sup>2&minus;</sup>","Peroxide#Inorganic chemistry|peroxide ion"); 

first, with awk, we can determine and prepend on each line the lenght of the first field, so after the first command (%!awk -F ',' '{print length($1) "|" $0}') your buffer will look like:

48|"Bi(NO<sub>3</sub>)<sub>3</sub>·5H<sub>2</sub>O","bismuth nitrate pentahydrate"); 
57|"Be(C<sub>5</sub>H<sub>7</sub>O<sub>2</sub>)<sub>2</sub>","Be acetylacetonate"); 
34|"O<sub>2</sub><sup>2&minus;</sup>","Peroxide#Inorganic chemistry|peroxide ion"); 
49|"Cu(BrO<sub>3</sub>)<sub>2</sub>·6H<sub>2</sub>O","Cu bromate hexahydrate");
49|"Mn(CHO<sub>2</sub>)<sub>2</sub>·2H<sub>2</sub>O","Mn formate dihydrate"); 

Then, we can sort it with the sort program, so, after the second command (%!sort -nr), your buffer would look like this

57|"Be(C<sub>5</sub>H<sub>7</sub>O<sub>2</sub>)<sub>2</sub>","Be acetylacetonate"); 
49|"Mn(CHO<sub>2</sub>)<sub>2</sub>·2H<sub>2</sub>O","Mn formate dihydrate"); 
49|"Cu(BrO<sub>3</sub>)<sub>2</sub>·6H<sub>2</sub>O","Cu bromate hexahydrate");
48|"Bi(NO<sub>3</sub>)<sub>3</sub>·5H<sub>2</sub>O","bismuth nitrate pentahydrate"); 
34|"O<sub>2</sub><sup>2&minus;</sup>","Peroxide#Inorganic chemistry|peroxide ion"); 

Then you clean your lines with %norm df| that way you end up with your lines sorted by first field length.

If it is just part of a bigger file, you can use visual mode to select the lines you want to sort and then press colon and the same commands but without the percent symbol. The commands then would start with: '<,'>. For example, the first command would look like '<,'>!awk -F, '{print length($1) "|" $0}'

This probably won't work on windows though

1
  • Clever. Nicely done.
    – Anon
    Mar 8, 2017 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.