namejm写的批处理
源代码在线查看: 对数字进行排序.bat
@echo off
:: 对数字由大到小排序
:: test.txt中的内容为一个数组:123,456,2,1,6……
:: 若文本内一行一个数字,则用:_sort一段代码就可以了,但是,
:: 需要注意的是, findstr /v ^!max!$ test.txt 并不能过滤掉最后一行内容
:: 除非最后一行是个空行。也就是说,findstr过滤行的时候,只能识别行分隔符号,
:: 没有行分隔符号,则不认为是一行。
:: code by jm 2006-10,4 CMD@XP
copy test.txt test.bak>nul
cd.>test.txt
for /f %%i in (test.bak) do call :split "%%i,:"
setlocal enabledelayedexpansion
:_sort
for /f %%i in (test.txt) do (
set /a max=%%i
for /f %%j in (test.txt) do (
set /a num=%%j
if !num! gtr !max! (set max=!num!) else (set max=!max!)
)
echo !max!
>test1.txt findstr /v ^!max!$ test.txt
move test1.txt test.txt
goto _sort
)
move test.bak test.txt
pause
goto :eof
:split
for /f "tokens=1* delims=," %%i in (%1) do (
>>test.txt echo %%i
if not "%%j"==":" call :split "%%j"
)
goto :eof
通过对创建的文件大小来对数字进行排序
code by 3742668 2006-10-4
fsutil file createnew %%i %%i语句执行速度太慢,换成for /l %%j in (1,1,%%i) do echo.>>%%i语句将使效率大为提升(-jm注)
@echo off
set numbers=%random% %random% %random% %random% %random% %random% %random% %random%
echo 排序前数字为: %numbers%
md sort_tmp && pushd sort_tmp
for %%i in (%numbers%) do fsutil file createnew %%i %%i >nul
echo 从小到大排列:
dir /os /b
popd && rd /s /q sort_tmp
pause
goto :eof
通过数字大小的比较来排序
但是 call :sort !str:%num%=! 一句会导致出错
@echo off
setlocal enabledelayedexpansion
set numbers=%random% %random% %random% %random% %random% %random% %random% %random%
echo 排序前数组为: %numbers%
call :sort %numbers%
echo 排序后数组为: %ret%
pause
goto :eof
:sort
set num=%1
set str=%*
for %%i in (%*) do if !num! gtr %%i set num=%%i
set ret=%ret% %num%
if not "!str:%num%=!" == "" call :sort !str:%num%=!
goto :eof