It is surprisingly easy and fast to accomplish this by writing each line as its own file within a temporary folder. Then use DIR /B /O-S to sort the files (lines) by size, capturing the result with FOR /F and then printing each file (line) with TYPE.
@echo off
setlocal disableDelayedExpansion
set "file=example.txt"
set "tempLoc=sortLinesTemp"
md "%tempLoc%"
set "cnt=0"
for /f usebackq^ delims^=^ eol^= %%A in ("%file%") do (
set /a cnt+=1
set "ln=%%A"
setlocal enableDelayedExpansion
echo(!ln!>"%tempLoc%\f!cnt!"
endlocal
)
(for /f %%F in ('dir /b /o-s "%tempLoc%"') do type "%tempLoc%\%%F")>"%file%.new"
move /y "%file%.new" "%file%" >nul
rd /s /q "%tempLoc%"
type "%file%"
This solution will strip empty lines. Empty lines can be preserved with a bit more code.
Also, line lengths are limited to a bit less than 8191 characters. This limitation is inherent to any pure native batch solution.