Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting on two columns in vim

Tags:

vim

sorting

I have a table that looks something like this:

FirstName SurName;Length;Weight;

I need to sort on length, and if the length is equal for one or more names, I need to sort those on weight. sort ni sorts only on length, I tried sort /.\{-}\ze\dd/ that too, but that didn't work either.

Any help would be greatly appreciated!

like image 918
Matthias Van Eeghem Avatar asked Oct 30 '12 21:10

Matthias Van Eeghem


People also ask

How do I sort two columns in Linux?

Use the -k option to sort on a certain column. For example, use " -k 2 " to sort on the second column. In old versions of sort, the +1 option made the program sort on the second column of data ( +2 for the third, etc.).

How do I sort a column in vim?

Sorting text in Vim is easy! Select the text, then press : , type sort , then hit enter! It'll sort the whole document by default, but you can enter a range too.

How do you sort a third column in Unix?

-k Option: Unix provides the feature of sorting a table on the basis of any column number by using -k option. Use the -k option to sort on a certain column.

Can you sort by two columns in Salesforce?

Select the first column that you want to sort and how you want to sort it. If you want to see the column names, select Use the first row as headers. To sort by another column, click Add Sort Column, choose the column, and select the sort conditions. Click Apply.


1 Answers

This can be done using an external (GNU) sort pretty straightforwardly:

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

This says: split fields by semicolon, sort by 2nd field numerically, then by 3rd field numerically. Probably a lot easier to read and remember than whatever vim-internal command you can cook up.

Much more info on GNU sort here: http://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html

like image 156
Kenan Banks Avatar answered Oct 22 '22 16:10

Kenan Banks