sorting strings easily

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

sorting strings easily

#1 Post by Ed Dyreen » 27 Oct 2011 20:21

'
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
:?:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: sorting strings easily

#2 Post by dbenham » 27 Oct 2011 21:24

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: sorting strings easily

#3 Post by Ed Dyreen » 27 Oct 2011 23:53

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: sorting strings easily

#4 Post by dbenham » 28 Oct 2011 09:44

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: sorting strings easily

#5 Post by Ed Dyreen » 28 Oct 2011 14:55

'
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 :?

Post Reply