Advertisement
T3RRYT3RR0R

Alphanumerical String Sort For txt files or Arrays

Sep 3rd, 2020 (edited)
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.37 KB | None | 0 0
  1. @Echo Off
  2. Set "Usage=Echo/&Echo/%~n0 ["/F:filepath.ext" ^| "/A:Array Group Name"] & Exit /B 1"
  3. IF "%~1"=="" %Usage%
  4. IF "%~1"=="/?" %Usage%
  5. Set "Inset=%~1"
  6. Set "Switch=%Inset:~1,1%"
  7. Call Set "Inset=%%Inset:/%Switch%:=%%"
  8. Echo/%Switch%|Findstr /LIC:F > Nul && (
  9.  (For /F "UseBackQ Delims=" %%G in (`Type "%Inset%" ^| Sort`)Do (Echo/%%~G)) 2> Nul || (Echo/Invalid Filepath:"%Inset%"&%Usage%)
  10.  Exit /B 0
  11. )
  12. If Not "!Comspec!"=="%Comspec%" (Echo/&Echo/Usage Error - Ensure Delayed Expansion is enabled with:&Echo/Setlocal EnableDelayedExpansion& Exit /B 1)
  13. Echo/%Switch%|Findstr /LIC:A > Nul && (For /F "Tokens=1,2 Delims==" %%G in ('Set %Inset%')Do Echo/%%H)>"%TEMP%\__Sort.txt"
  14. Set "#$#=0"
  15. (For /F "UseBackQ Delims=" %%G in (`Type "%TEMP%\__Sort.txt" ^| Sort`)Do (
  16.   Set "%Inset%[!#$#!]=%%~G"
  17.   Set /A "#$#+=1"
  18.  )
  19. )
  20. Del /Q "%TEMP%\__Sort.txt"
  21. Exit /B 0
  22.  
  23. Rem ::: Sorts STRINGS alphanumericaly. Not suited to Integer strings of varying length.
  24. Rem ::: Can Sort text files or Arrays
  25. Rem ::: With the manner sort process strings, when numerical strings are sorted Single digits will be treated as a higher value than multiple digits that
  26. Rem ::: begin with a lesser value, as the sort is applied on the basis of character value in the string position, not the overall strings value.
  27. Rem ::: For a given position in a string Character sort order is: [0-9][a-z]
  28. Rem ::: Case is ignored
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement