udfFileSort
int udfFileSort (str, str, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileSort (strFilenameIn, strFilenameOut, intDirection)
If !FileSize (strFilenameIn) Then Return 0
Select intDirection
Case @ASCENDING
   Return (udfItemListToFile (strFilenameOut, ItemSort (udfItemListFromFile (strFilenameIn, @LF), @LF), @LF))
Case @DESCENDING
   Return (udfItemListToFile (strFilenameOut, udfItemListReverse (ItemSort (udfItemListFromFile (strFilenameIn, @LF), @LF), @LF), @LF))
EndSelect
Return 0
;..........................................................................................................................................
; This UDF udfFileSort sorts lines of a textfile with EOL=CRLF in ascending or descending direction.
; Return value is the number of bytes written to the outfile.
; Example: intResult = udfFileSort (strMyFileIn, strMyFileOut, @ASCENDING)
;
; This UDF udfFileSort calls three other UDFs:
; - udfItemListFromFile (strFilename, strDelimiter)
; - udfItemListToFile (strFilename, strItemList, strDelimiter)
; - udfItemListReverse (strItemList, strDelimiter)
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListToFile (strFilename, strItemList, strDelimiter) ; The FilePut version.
If strItemList == "" Then Return 0
Return FilePut (strFilename, StrReplace (strItemList, strDelimiter, @CRLF))
;..........................................................................................................................................
; This UDF udfItemListToFile writes an itemlist to a textfile with EOL=CRLF and returns the number of bytes written.
; Example: intResult = udfItemListToFile (strMyFile, @TAB, strMyList)
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListFromFile (strFilename, strDelimiter) ; The FileGet version.
If !FileSize (strFilename) Then Return ""
If strDelimiter == "" Then strDelimiter = @TAB
strItemList = StrReplace (StrReplace (FileGet (strFilename), @CRLF, @LF), @CR, "") ; @CR has no meaning.
If strDelimiter != @LF Then strItemList = StrReplace (strItemList, @LF, strDelimiter)
Return strItemList
;..........................................................................................................................................
; This UDF udfItemListFromFile reads a textfile line by line with EOL=LF or EOL=CRLF and creates an itemlist accordingly.
; Example: strMyList = udfItemListFromFile (strMyFile, @TAB)
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListReverse (strItemList, strDelimiter)
strItemListReverse = ""
intCount = ItemCount (strItemList, strDelimiter)
For intElem = intCount To 1 By -1
   strItemListReverse = ItemInsert (ItemExtract (intElem, strItemList, strDelimiter), -1, strItemListReverse, strDelimiter)
Next
Return strItemListReverse
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this script as input file.
strFilenameTemp = FileCreateTemp ("TMP")

Display (4, "Demo: udfFileTextSort (strFilename) / @ASCENDING ", "Blank lines are sorted on top of the file!")
intResult = udfFileSort (strFilename, strFilenameTemp, @ASCENDING) ; Read this script and sort it to tempfile.
RunZoomWait ("notepad.exe", strFilenameTemp) ; Take a look, be aware that the blank lines are sorted on top of the file.

Display (4, "Demo: udfFileTextSort (strFilename) / @DESCENDING", "Blank lines are sorted on bottom of the file!")
intResult = udfFileSort (strFilenameTemp, strFilenameTemp, @DESCENDING) ; Read tempfile and sort it to tempfile (file will be overwritten).
RunZoomWait ("notepad.exe", strFilenameTemp) ; Take a look and wait for closing notepad.

blnResult = FileDelete (strFilenameTemp)
Exit