Thursday, August 26, 2010

Re: sort words within a line

Hi Tom!

On Di, 24 Aug 2010, Tom wrote:

> On Tue, Aug 24, 2010 at 09:51:56PM +0200, Christian Brabandt wrote:
> > Hi Tom!
> >
> > On Di, 24 Aug 2010, Tom wrote:
> >
> > > How does one sort words with a line ? As example I have these numbers in a
> > > line:
> > >
> > > 400, 250, 125, 600
> > >
> > > I want to sort them so they will be 125, 250, 400, 600
> > >
> > > I have tried the visual command then !sort but that doesnt do it.
> >
> > :call setline(line('.'),join(sort(split(getline('.'))), ' '))
> >
> > regards,
> > Christian
>
> Amazing amazing that works also. Seems there is always more than one way to
> skin a cat.

There is still room for improvement. For example if you consider this
line:
400, 250, 125, 600, 100
this would be sorted to:
100 125, 250, 400, 600,
(which looks not right if you look at the commas).

So here is a slightly changed version:
:call setline('.',join(sort(split(substitute(getline('.'), ',', '', 'g'))), ', '))

(one line)

Basically, this replaces the comma and adds it later back.

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: