How to sort numeric variables ascending in DOS Batch?
ex.
set number1=4
set number2=1
set number3=5
set number4=2
set number5=3
How to create a variable with sorted numbers
Result 1 2 3 4 5
How to sort numeric variables ascending in DOS Batch?
Moderator: DosItHelp
-
- Posts: 31
- Joined: 08 Sep 2017 06:10
Re: How to sort numeric variables ascending in DOS Batch?
This solves your example in terms of input and desired output:The example is limited. If your requirement is more than unsigned digits from 0-9, please define "number". If your number is on the left of the "=" then it is already sorted.
John A.
Code: Select all
@echo off &setlocal EnableDelayedExpansion
set number1=4
set number2=1
set number3=5
set number4=2
set number5=3
>%Temp%\%~n0.txt Echo.
For /L %%N in (1,1,5) Do >>%Temp%\%~n0.txt Echo.!number%%~N!
Sort %Temp%\%~n0.txt /o %Temp%\%~n0.txt
For /F %%N in ('Type %Temp%\%~n0.txt') Do Set "numbers=!numbers! %%~N"
Echo.Result%numbers%
John A.