China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-22 18:07
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Numerical Sorting (Difficulty: Difficult, Problem Solved) DigestI View 12,667 Replies 31
Floor 16 Posted 2008-04-30 08:37 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Hehe, didn't expect that number sorting actually caused the second storm in the union. It's lively.

Also post a piece of previous code to add some fun
:

@echo off&goto start&rem 数字排序
Numbers are in a.txt, one per line
Can sort any integer within 200 digits. (Including negative numbers, positive numbers, 0, and repeated numbers)
Disadvantage: For numbers starting with 0, the leading 0 will be ignored in the result
::by 26933062 2007-11-25
:start
setlocal EnableDelayedExpansion
for /l %%a in (1 1 200) do set lin=0!lin!
for /f %%a in (a.txt) do (
set "str=%%a"
if "!str:~0,1!"=="-" (set fus=a&set str=!str:~1!&set zf=_) else set zf=+
set str=!lin!!str!
set !zf!!str:~-200! !random!!random!!random!!random!!random!!random!!random!=a
)
if defined fus call :sort _ /r
call :sort +
pause&exit
:sort
for /f "tokens=1,2 delims=_+= " %%a in ('set %1^|sort %2') do (
for /f "tokens=* delims=0" %%i in ("%%a") do (
if "%1"=="_" (set fuhao=-) else set "fuhao="
if "%%i"=="" (echo !fuhao!0) else echo !fuhao!%%i
)
)
goto :eof
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
plp626 +7 2008-05-04 22:01
致精致简!
Floor 17 Posted 2008-04-30 10:13 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
It can range from negative numbers to positive numbers, and it will remove duplicate numbers. It cannot correctly handle numbers that start with 0.
@echo off
setlocal enabledelayedexpansion
set str= %random% %random% -%random% %random% %random% -%random% %random% -%random% %random% %random%
:: For a line - by - line number in a.txt, use the next line to read, and do not need the previous line
:: The number before the previous line set str= %random%... must have a space in front
::for /f %%i in (a.txt) do set str=!str! %%i
echo The array before sorting is: %str:~1%

:sort
set num=m
for %%i in (%str%) do if %%i lss !num! set num=%%i
set ret=%ret% %num%
set str=!str: %num%=!
if not "%str%" == "" goto sort

echo The array after sorting is: %ret:~1%
pause
exit
Floor 18 Posted 2008-04-30 16:01 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Simplify the terse scheme, and we can modify it if there are deficiencies.


@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!=%%i
)
for /f "tokens=2 delims==" %%i in ('set .') do set/p=%%i <nul
pause>nul
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
plp626 +5 2008-04-30 16:27
Floor 19 Posted 2008-04-30 17:25 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
So lively, I'll also add a part. The drawback is that the efficiency is a bit low...

@echo off & Setlocal EnableDelayedExpansion

for /L %%i in (1,1,5) do (
set skip=.!skip!
findstr /rc:"^^!skip!$" file.txt|sort
)


In the for, 5 is specified according to the maximum number of digits. If you don't know, you can also write a for statement to calculate it. I omitted it here.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
plp626 +15 2008-04-30 17:28
Floor 20 Posted 2008-04-30 18:02 ·  中国 江苏 苏州 移动
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
Originally posted by lxmxn at 2008-4-30 05:25 PM:
So lively, I'll also add one, the shortcoming is that the efficiency is a bit low...
@echo off & Setlocal EnableDelayedExpansion

for /L %%i in (1,1,5) do (
set skip=.!skip!
findstr /rc:"^^!ski ...

I didn't understand the moderator's code, can you explain it?
It seems that negative number sorting is not supported
Floor 21 Posted 2008-04-30 23:14 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by huahua0919 at 2008-4-30 18:02:

I didn't understand the moderator's code. Can you explain it?
It seems that negative numbers are not supported for sorting

It's simply to find numbers from 1-digit to 5-digit using findstr according to the number of digits, and then sort them with sort. But the premise is that the numbers are all positive integers and the maximum number of digits is known. Strictly speaking, this is not sorting~
Floor 22 Posted 2008-05-02 05:09 ·  中国 北京 海淀区 联通
初级用户
Credits 28
Posts 15
Joined 2008-04-28 09:18
18-year member
UID 117150
Gender Male
Status Offline
Why add a long string of zeros in front of a number, and then when traversing in the environment variable, the zeros are truncated, and they can be automatically sorted? Please give your advice, great experts.
Floor 23 Posted 2008-05-02 11:26 ·  中国 北京 海淀区 联通
初级用户
Credits 28
Posts 15
Joined 2008-04-28 09:18
18-year member
UID 117150
Gender Male
Status Offline
Why is nobody replying to me?
Floor 24 Posted 2008-05-02 12:14 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
99
1
888

The sorting result is
1
888
99

If padding with zeros, the result is:

00001
00099
00888

……
Floor 25 Posted 2008-05-02 12:23 ·  中国 北京 海淀区 联通
初级用户
Credits 28
Posts 15
Joined 2008-04-28 09:18
18-year member
UID 117150
Gender Male
Status Offline
Originally posted by MLQX at 2008-5-2 11:26 AM:
Thanks.
[ Last edited by MLQX on 2008-5-2 at 12:34 PM ]
Floor 26 Posted 2008-05-02 12:45 ·  中国 北京 海淀区 联通
初级用户
Credits 28
Posts 15
Joined 2008-04-28 09:18
18-year member
UID 117150
Gender Male
Status Offline
Originally posted by zh159 at 2008-4-30 01:51 AM:
Used before, forgot where it came from
@echo off
setlocal enabledelayedexpansion
set numbers=%random% %random% %random% %random% %random% %random% %random% %random%
echo 排序前 ...


Learned from this forum for 3 days, today I finally can understand this code.
In my eyes as a newbie, this way of thinking is very unique.
In other people's eyes, maybe not unique, but classic.
Floor 27 Posted 2008-05-02 12:53 ·  中国 北京 海淀区 联通
初级用户
Credits 28
Posts 15
Joined 2008-04-28 09:18
18-year member
UID 117150
Gender Male
Status Offline
Originally posted by plp626 at 2008-4-29 11:59 PM:
if not %1.==. 


Is it an implicit end-of-file mark in a text file? Or is it a mark indicating the beginning or end of a line?

[ Last edited by MLQX on 2008-5-2 at 01:24 PM ]
Floor 28 Posted 2008-05-03 22:19 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
&&Unexpectedly, a single stone stirs up thousands of waves. I haven't been to the forum for a few days, and this post has become an essence post and is so popular, which is really beyond my initial expectation. Today, I studied my original code again and have the following highly universal code:

@echo off&setlocal enabledelayedexpansion
set "str=101 20 14 13 19 18 16 11 5 -7"
for %%i in (%str%) do call,set var=!var! n%%i
for %%i in (%var%) do set a=%%i&call :lp
echo !var:n=!
pause>nul&goto :eof
:lp
for %%i in (!var!) do (
set b=%%i
if !b:~1! lss %a:~1% (
set var=!var:%a% =!
set var=!var:%%i=%%i %a%!
)
)
批处理之家新域名:www.bathome.net
Floor 29 Posted 2008-05-04 00:41 ·  中国 北京 海淀区 联通
初级用户
Credits 28
Posts 15
Joined 2008-04-28 09:18
18-year member
UID 117150
Gender Male
Status Offline
@echo off&for /f "delims=" %%i in ('sort /+n c.txt') do echo %%i

If comparing three-digit numbers, change the n value to 3.
Floor 30 Posted 2008-05-04 12:02 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by zw19750516 at 2008-5-3 22:19:
&&Unexpectedly, a single stone stirs up a thousand waves. I haven't been to the forum for a few days, and this post has become an essence post, and it's so popular. It's really beyond my original expectation. Today, I studied my original code again and have the following...

Not universal. You try set "str=1111 111 20 14 13 19 18 16 11 5 -7"
简单!简单!再简单!
Forum Jump: