Re: sort words within a line

2010-08-26 Thread Christian Brabandt
Hi Jürgen!

On Do, 26 Aug 2010, Jürgen Krämer wrote:

> Christian Brabandt wrote:
> > 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.
> 
> you can take advantage of the second, optional argument to split() and
> save the call to substitute():
> 
>   :call setline(line('.'),join(sort(split(getline('.'), ',\s*')), ', '))
> 
> This splits the line on every comma optionally followed by white-space.
> If there is a trailing comma, split() automatically removes it. Like in
> your second version the comma has to be added back later.

True, I didn't think of that version.

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


Re: sort words within a line

2010-08-26 Thread Jürgen Krämer

Hi,

Christian Brabandt wrote:
> 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.

you can take advantage of the second, optional argument to split() and
save the call to substitute():

  :call setline(line('.'),join(sort(split(getline('.'), ',\s*')), ', '))

This splits the line on every comma optionally followed by white-space.
If there is a trailing comma, split() automatically removes it. Like in
your second version the comma has to be added back later.

Regards,
Jürgen

-- 
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

-- 
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


Re: sort words within a line

2010-08-26 Thread Christian Brabandt
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


Re: sort words within a line

2010-08-24 Thread Tom
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.

Thanks Christian

Tom

-- 
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


Re: sort words within a line

2010-08-24 Thread Christian Brabandt
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

-- 
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


Re: sort words within a line

2010-08-24 Thread Tim Chase

On 08/24/10 14:10, 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.


Generally one uses a "decorate-sort-undecorate" pattern to do 
this sort of thing.  There are a couple edge-cases that you'll 
want to take note of:


1) all your example numbers are 3 digits.  Do you want them to 
sort numerically or lexicographically (should "111" come before 
"12"?)


2) you have commas between them and happen to end with the 
largest.  Do you expect "5, 4, 3, 2, 1" to sort to "1, 2, 3, 4, 
5" or "1 2, 3, 4, 5,"?



The DSU pattern would look something like

  :y " yank the current line
  :new   " create a junk buffer
  :0put  " dump the line
  :$d" delete the extra blank line
  :s/,\s*/\r/g   " put each on its own line
  :sort  " sort them lex. or
  :sort n" sort them numerically
  :1,$-s/$,  " put the commas back in
  :%j" rejoin into one line
  :d " cut the line
  :q!" quit the scratch buffer
  :put   " put it below the target line
  :-d" delete the previous unsorted line

which could all be rolled up into a mapping or function.  Note 
that it does tromp your scratch register, but a little care can 
prevent that by saving & restoring it.


-tim





--
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