Here is an approach that can handle numeric values with up to eight digits and even duplicate ones; it relies on the sorting capabilities of the set command:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1"
rem /* Store all lines into an array-style variable set `ARRAY[]` with
rem the left-zero-padded time value as the first index, the current
rem line number as the second index to avoid loss of duplicate values
rem and the current line string are the value: */
for /F "tokens=1* delims== eol==" %%K in ('findstr /N "^" "%_FILE%"') do (
set "KEY=%%K" & set "VAL=%%L"
setlocal EnableDelayedExpansion
set /A "NUM=100000000+VAL, LIN=100000000+KEY"
set "KEY=!KEY:*:=!"
for /F "delims=" %%E in ("ARRAY[!NUM:~1!_!LIN:~1!]=!KEY!=!VAL!") do (
endlocal
set "%%E"
)
)
rem // Return values of the array elements sorted by their index:
for /F "tokens=1* delims==" %%K in ('2^> nul set ARRAY[') do (
echo(%%L
)
endlocal
exit /B
Here is an alternative approach using the sort command and a pipe |:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1"
rem /* Precede every line with the sum of `900000000` and the time value,
rem so the resulting number always consists of nine digits, given that
rem the value is less than `100000000`; the prefix is separated from
rem the original text file string by a `|` character; the resulting
rem text is fed into another block of code via a pipe `|`; since pipes
rem instantiate new `cmd` instances for either side, and each side is
rem executed under `cmd` context, the `set /A` command, in contrast to
rem batch file context, returns the (last) result without a trailing
rem line-break, so the text of the next `echo` command is appended;
rem the augmented lines are fed into the `sort` command via a pipe `|`,
rem the prefix is split off using the `|` character (which the original
rem line text must not begin with); `sort` does pure string sorting,
rem but since the number of digits of the prefix is always the same,
rem the alphabetic (string) order equals the numeric order; finally;
rem the prefix is removed from the rest, which is the original text: */
(
rem/ // Read the text file line by line, disregard empty lines:
for /F "usebackq tokens=1* delims== eol==" %%K in ("%_FILE%") do @(
rem/ // Extract the numeric part, add "900000000", return result:
set "VAL=%%L" & set /A "900000000+VAL"
rem/ // Append "|", followed by the original line string:
echo(^^^|%%K=%%L
)
) | (
rem/ /* Feed augmented text into "sort", read output line by line,
rem/ split off "|" and everything in front: */
for /F "tokens=1* delims=|" %%K in ('sort') do @(
rem/ // Return original line string:
echo(%%L
)
)
endlocal
exit /B
sortdoes a strict string sorting. It isn't able to recognize numbers. So if you want to sort by numbers, you'll have to code your own logic or find/use some external utility.>character and spaces is totally confusing. Consider this point in your future questions...