Two-column sort in vim

I have a table that looks something like this:

  FirstName SurName; Length; Weight;

I need to sort by length, and if the length is equal to one or more names, I need to sort them by weight. sort ni sorted only by length, I also tried sort /.\{-}\ze\dd/ , but that didn't work either.

Any help would be greatly appreciated!

+7
source share
2 answers

This can be done using the external (GNU) kind of pretty simple:

 !sort -t ';' -k 2,2n -k 3,3n 

This says: split fields by semicolons, sort by the second field numerically, and then by the 3rd pole numerically. It is probably much easier to read and remember than any vim-internal command you can prepare.

Much more information about GNU can be found here: http://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html

+6
source

Try with the r flag.

Sort by length:

 :%sort rni /.*;\ze\d/ 

Sort by Weight:

 :%sort rni /\d+\ze;$/ 

Without this flag, sorting is done according to what happens after the match, which can be a bit cumbersome.

With the r flag, sorting is done in the same match, which might be easier to determine. Here the pattern corresponds to a series of 1 or more digits immediately before the semicolon at the end of the line.

+1
source

All Articles