r/Batch • u/thelowsunoverthemoon • Apr 10 '21
Show 'n Tell Easy String Sorting in Batch
Sorting strings is typically a painful task in Batch, but not with the SORT command. With the help of macros, you can make string sorting look very neat & tidy, almost like .filter methods in other programming languages. This is mostly syntactic sugar, but since it's only a simple variable using string substitution, I doubt there's going to be a measurable performance hit. Here they are:
SET "sort[fwd]=(FOR %%S in (%%#%%) DO @ECHO %%S) ^| SORT"
SET "sort[rev]=(FOR %%S in (%%#%%) DO @ECHO %%S) ^| SORT /R"
SET "filter[fwd]=(FOR %%F in (%%#%%) DO @ECHO %%F) ^| SORT /UNIQ"
SET "filter[rev]=(FOR %%F in (%%#%%) DO @ECHO %%F) ^| SORT /UNIQ /R"Given an array, all you have to do is "substitute" them in. For example:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sort[rev]=(FOR %%S in (%%#%%) DO @ECHO %%S) ^| SORT /R"
SET "array=hello bye zero cool zack well this this cool augment bye"
%sort[rev]:#=array%
PAUSE
EXIT /BWill sort the contents of array alphabetically reversed. All you have to do is copy and paste the ones you're using into your script. This is what each one does:
sort[fwd] sorts the array alphabetically
sort[rev] sorts the array alphabetically reversed
filter[fwd] filters out the unique values of the array, then sorts alphabetically
filter[rev] filters out the unique values of the array, then sorts alphabetically reversed
But what happens if you want to put the output into a file? You can either redirect the entire command
(%filter[rev]:#=array%)>filtered.txtOr add the /O switch at the end
%filter[rev]:#=array% /O filtered.txtHow about if you want to do something to the sorted values? Lets say if you wanted to create variables for each value. All you have to do is escape the closing parenthesis using a caret. Ie here are the FOR /F "loop" versions:
SET "lsort[fwd]=(FOR %%S in (%%#%%^) DO @ECHO %%S^) ^| SORT"
SET "lsort[rev]=(FOR %%S in (%%#%%^) DO @ECHO %%S^) ^| SORT /R"
SET "lfilter[fwd]=(FOR %%F in (%%#%%^) DO @ECHO %%F^) ^| SORT /UNIQ"
SET "lfilter[rev]=(FOR %%F in (%%#%%^) DO @ECHO %%F^) ^| SORT /UNIQ /R"Example use:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "lfilter[rev]=(FOR %%F in (%%#%%^) DO @ECHO %%F^) ^| SORT /UNIQ /R"
SET "num=0"
SET "array=hello bye zero cool zack well this this cool augment bye"
FOR /F %%Q in ('%lfilter[rev]:#=array%') DO (
SET "array[!num!]=%%Q"
SET /A "num+=1"
)
SET array
PAUSE
EXIT /BCreates numbered variables for each sorted word. Here's a script that showcases each of the (non loop version) macros:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sort[fwd]=(FOR %%S in (%%#%%) DO @ECHO %%S) ^| SORT"
SET "sort[rev]=(FOR %%S in (%%#%%) DO @ECHO %%S) ^| SORT /R"
SET "filter[fwd]=(FOR %%F in (%%#%%) DO @ECHO %%F) ^| SORT /UNIQ"
SET "filter[rev]=(FOR %%F in (%%#%%) DO @ECHO %%F) ^| SORT /UNIQ /R"
SET "array=hello bye zero cool zack well this this cool augment bye"
ECHO Original
FOR %%Q in (%array%) DO (
ECHO %%Q
)
ECHO/
ECHO Sorted Normal
%sort[fwd]:#=array%
ECHO/
ECHO Sorted Reversed
%sort[rev]:#=array%
ECHO/
ECHO Filtered Normal
%filter[fwd]:#=array%
ECHO/
ECHO Filtered Unique
%filter[rev]:#=array%
ECHO/
PAUSE
EXIT /BHave fun,
Lowsun