感谢Aacini,我现在有办法将变量从最大到最小排序。
链接: Comparing and ordering multiple numbers in batch
但是,如果我的两个或多个变量的值相同,则不会对它们进行排序。我正在尝试测试以查看集合中的两个变量是否相等。我试着以我能想到的任何组合对每个变量使用if语句,但这不是很有效且很难改变。
有没有办法实现这个目标?
@echo off
setlocal EnableDelayedExpansion
set speed1=190
set speed2=78
set speed3=78
set speed4=23
rem Get the descending order of previous elements via "order" array
for /L %%i in (1,1,4) do (
set /A num=1000-speed%%i
set order!num!=%%i
)
rem Show the elements of "speed" array in descending order
for /F "tokens=2 delims==" %%i in ('set order') do (
echo speed%%i = !speed%%i!
)
输出仅显示:
speed1 = 190
speed3 = 78
speed4 = 23
答案 0 :(得分:1)
对不起。我不知道你是否真的有兴趣知道两个元素是否具有相同的值,或者只是为了修复我之前的解决方案的错误(不包含具有相同值的元素),所以我选择解决之前的问题错误:
@echo off
setlocal EnableDelayedExpansion
set speed1=190
set speed2=78
set speed3=78
set speed4=23
rem Get the descending order of previous elements via "order" array
REM Insert a second index to differentiate elements with the same value
for /L %%i in (1,1,4) do (
set /A num=1000-speed%%i
set order[!num!][%%i]=%%i
)
rem Show the elements of "speed" array in descending order
for /F "tokens=2 delims==" %%i in ('set order') do (
echo speed%%i = !speed%%i!
)