中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-08 22:15
47,811 topics / 349,895 posts / today 0 new / 48,253 members
DOS批处理 & 脚本技术(批处理室) » [Help] Numerical Sorting (Difficulty: Difficult, Problem Solved)
Printable Version  13,119 / 31
Floor1 bat-zw Posted 2008-04-29 22:22
金牌会员 Posts 1,276 Credits 3,105
If there is a column of values such as: 56 12 33 48 99 105 5 16
How to output it as: 5 12 16 33 48 56 99 105 through batch processing, and no temporary files are to be generated.
----------------------------------------------------------------------------------------------------------
Overall, my general code is as follows:

Further expansion:


[ Last edited by zw19750516 on 2008-5-4 at 09:20 PM ]
Floor2 qingfushuan Posted 2008-04-29 22:40
高级用户 Posts 327 Credits 502
Arrange them in order from small to large.
Floor3 HAT Posted 2008-04-29 22:44
版主 Posts 5,017 Credits 9,023
If it's purely P, I usually first compare the lengths, and if the lengths are the same, then compare the values.
Floor4 bjsh Posted 2008-04-29 23:21
银牌会员 Posts 621 Credits 2,000
Linux

$ cat TEST
56
12
33
48
99
105
5
16

$ sort -n <TEST
5
12
16
33
48
56
99
105
Floor5 plp626 Posted 2008-04-29 23:34
银牌会员 Posts 1,020 Credits 2,278
The moderator also comes to join in the fun...

cmd is not as powerful as shell.
Floor6 bat-zw Posted 2008-04-29 23:37
金牌会员 Posts 1,276 Credits 3,105
There is still no perfect answer so far!
Floor7 terse Posted 2008-04-29 23:56
银牌会员 Posts 946 Credits 2,404
@echo off&setlocal enabledelayedexpansion
set/p str=Enter a number (separated by spaces)
for %%i in (%str%) do (
set str=0000000000%%i
set .!str:~-10! !random!=a
)
for /f "delims=.= " %%i in ('set .') do for /f "tokens=* delims=0" %%i in ("%%i") do if "%%i"=="" (set/p=0 <nul) else set/p=%%i <nul
pause>nul
Floor8 plp626 Posted 2008-04-29 23:59
银牌会员 Posts 1,020 Credits 2,278
```
@echo off&if not %1.==. goto%*
::plp@cn-dos.net|08-4-29
::Number sorting. Number range: (0-10^99) Numbers are saved in dat.txt, one number per line
if not exist dat.txt echo Missing dat.txt&pause&exit
for /f "delims= " %%a in ('"%0" :sor^|sort')do echo %%a
pause
:sor
setlocal enabledelayedexpansion
for /f %%a in (dat.txt)do (set d=%%a
call:byte&call:space
echo\!sp!!d!
)
exit/b
:byte
for /l %%z in (1 1 100)do if "!d:~%%z,1!"=="" set/a byte=100-%%z&goto:eof
:space
set "sp="
for /l %%z in (1 1 %byte%)do set "sp=!sp! "
goto:eof

Numbers are not one per line. First use this code to change it to one per line


[ Last edited by plp626 on 2008-4-30 at 01:41 PM ]
```
Floor9 HAT Posted 2008-04-30 00:01
版主 Posts 5,017 Credits 9,023
I wonder if brother s11ss's sorting algorithm can meet the building owner's needs

Floor10 bat-zw Posted 2008-04-30 00:04
金牌会员 Posts 1,276 Credits 3,105
Depressed, the single digits cannot have duplicates:
Floor11 terse Posted 2008-04-30 00:11
银牌会员 Posts 946 Credits 2,404
Is my post on floor 8 not meeting the requirements?
Change `set/p str=输入数字(用空格隔开)` to `set "str=101 20 14 13 19 18 16 11 5"`
It should be okay, right?
Floor12 bat-zw Posted 2008-04-30 00:18
金牌会员 Posts 1,276 Credits 3,105
Originally posted by terse at 2008-4-30 00:11:
Is the code on my 8th floor not meeting the requirements?
Change "set/p str=Enter numbers (separated by spaces)" to "set "str=101 20 14 13 19 18 16 11 5"
It should be okay, right?

Terse's code on the 8th floor is really powerful. To be honest, I still don't understand how it works. Brother Terse, can you help explain it?
Floor13 HAT Posted 2008-04-30 00:32
版主 Posts 5,017 Credits 9,023
Originally posted by plp626 at 2008-4-30 12:04 AM:
Why use cmd when you can have the convenience, speed, and efficiency of a Linux shell?
Disclosure: I don't understand Linux.

This doesn't require understanding Linux; you just need to download GNU's sort.exe and call it in a batch script.
Floor14 pusofalse Posted 2008-04-30 00:39
银牌会员 Posts 646 Credits 1,604
Originally posted by terse at 2008-4-29 11:56 PM:
@echo off&setlocal enabledelayedexpansion
set/p str=Enter a number (separated by spaces)
for %%i in (%str%) do (
set str=0000000000%%i
set .!str:~-10! !random!=a
)
for /f "delims=.= " ...

Really powerful! I didn't understand it at first...
@echo off&setlocal enabledelayedexpansion
set/p str=Enter a number (separated by spaces)
for %%i in (%str%) do (
set str=0000000000%%i
set .!str:~-10! !random!=a
)
set .
pause
for /f "delims=.= " %%a in ('set .') do for /f "tokens=* delims=0" %%i in ("%%a") do if "%%i"=="" (set/p=0 <nul) else set/p=%%i <nul
pause>nul
After changing the code like this, I got a bit enlightened.
Floor15 zh159 Posted 2008-04-30 01:51
金牌会员 Posts 1,467 Credits 3,687
I used this before, but I forgot where it came from.
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023