I have 4 variables:
speed1=190
speed2=78
speed3=98
speed4=23
Is there any way to order the variables so I can display them from greatest to least?
For example, I want the above to display:
speed1=190
speed3=98
speed2=78
speed4=23
I have 4 variables:
speed1=190
speed2=78
speed3=98
speed4=23
Is there any way to order the variables so I can display them from greatest to least?
For example, I want the above to display:
speed1=190
speed3=98
speed2=78
speed4=23
There are several ways to solve this problem. You may use any sort method to get the ascending order of the array elements. However, in the particular case of Batch file programming, you may use a simple trick that make good use of the fact that environment variables are always kept in sorted order. When a new variable is defined the SET command place it in the right place, so all variables are sorted alphabetically.
To use this trick, just insert the desired index in the name of a new array. At end, process array elements in the natural order shown by SET command:
@echo off
setlocal EnableDelayedExpansion
set speed1=190
set speed2=78
set speed3=98
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!
)
I suggest you to read: Arrays, linked lists and other data structures in cmd.exe (batch) script