Page 1 of 1

sorting strings easily

Posted: 27 Oct 2011 20:21
by Ed Dyreen
'
Is there an easy way to do that, or do we have to use a char map or tempfile for that.

Code: Select all

echo. C, B, A |%sort%
>A, B, C
:?:

Re: sorting strings easily

Posted: 27 Oct 2011 21:24
by dbenham
I started a thread on this very subject a while ago: Sort tokens within a string & Disable FOR /F EOL option
My first solution in the initial post I still like, but it has limitations with * and ?

The 1st response by amel27 has 2 great solutions, one a variant on my solution that eliminates the wildcard limitation, and then a 2nd that I thought was very novel that has a limitation with =.

Dave Benham

Re: sorting strings easily

Posted: 27 Oct 2011 23:53
by Ed Dyreen
'
I also like your first solution, I don't think I'll be needing the second solution for now.

I would have never figured that out, as sort does not sort outside of a for.
This makes me wonder how you found out.
because echo. C, B, A |sort simply gives >C, B, A.

I also don't understand why leaving out @ on echo suddenly shows the path, why is that ?

Code: Select all

for /f %%a in ('^(for /f %%t in ^(!str!^) do @echo %%t^)^|sort %~2') do set "sorted=!sorted! %%a"
I created a macro on it:

Code: Select all

for /f %%? in ( %$n1c%
   '^^^^^( for %%? in ^^^^^(^^^!$str^^^!^^^^^) do @echo %%?^^^^^)^^^^^|sort' %$n1c%
) do    set "$sorted=^!$sorted^! %%?" %$n1c%

Thanx.

Re: sorting strings easily

Posted: 28 Oct 2011 09:44
by dbenham
Ed Dyreen wrote:I also don't understand why leaving out @ on echo suddenly shows the path, why is that ?

I don't understand why, but somehow the DO commands are echoed if the entire FOR loop is within a block (parentheses), and the block is on the left of a pipe.

Code: Select all

@echo off
(for %%t in (c b a) do echo %%t)|more

output:

Code: Select all

P:\>echo c
c

P:\>echo b
b

P:\>echo a
a


Both of the following fix the problem:

Code: Select all

@echo off
(for %%t in (c b a) do @echo %%t)|more

Code: Select all

@echo off
(echo off & for %%t in (c b a) do @echo %%t)|more


Dave Benham

Re: sorting strings easily

Posted: 28 Oct 2011 14:55
by Ed Dyreen
'
Maybe it has to do with the fact that @echo ?)|more creates a new process and that somehow @echo is turned on or maybe it's just another bug :?