udfArraySortDim2
arr udfArraySortDim2 (arr, int, int, str, str)
arr udfArrayDisplay (int, int, int, int, str, arr, str, str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArraySortDim2 (arrArray, intSortCol, intDirection, strDelimCol, strDelimRow)
If ArrInfo (arrArray, 0) != 2 Then Return arrArray ; No Dim2-array.
intDim1 = ArrInfo (arrArray, 1)
If !intDim1 Then Return arrArray ; No Elements.

intDim2 = ArrInfo (arrArray, 2)
intSortCol = Min (Max (-intDim2, intSortCol), intDim2 - 1) ; Force intSortCol to array bounds.
If intSortCol < 0 Then intSortCol = intDim2 + intSortCol ; Wrap around bounds. For last column use intSortCol=-1.

intPadLen = 0
intLast = intDim1 - 1
For intRow = 0 To intLast
   If IsDefined (arrArray[intRow, intSortCol]) Then intPadLen = Max (intPadLen, StrLen (arrArray[intRow, intSortCol]))
Next
If !intPadLen Then Return arrArray

If strDelimCol == "" Then strDelimCol = @TAB
   Else strDelimCol = StrSub (strDelimCol, 1, 1)
If strDelimRow == "" Then strDelimRow = @LF
   Else strDelimRow = StrSub (strDelimRow, 1, 1)

strFileTemp = FileCreateTemp ("ARR")
intPrevIC53 = IntControl (53, 2, 0, 0, 0) ; Set EOL to @LF.
ArrayFilePutCSV (strFileTemp, arrArray, strDelimCol, @TRUE, 2)
IntControl (53, intPrevIC53, 0, 0, 0) ; Set EOL back to previous value.
strArrList = FileGet (strFileTemp, " ") ; "7#c#aa@LF41#d#ee@LF2#a#ii@LF33#b#oo@LF" ; Note the trailing @LF.
strArrList = ItemRemove (-1, strArrList, strDelimRow) ; Remove trailing @LF.
intArrCount = ItemCount (strArrList, strDelimRow)
strTempList = ""
For intRow = 1 To intArrCount
   strArrRow = ItemExtract (intRow, strArrList, strDelimRow)
   strArrCol = ItemExtract (intSortCol + 1, strArrRow, strDelimCol)
   If IsNumber (strArrCol)
      strArrCol = StrFixLeft (strArrCol, " ", intPadLen)
   Else
      strArrCol = StrFix (strArrCol, " ", intPadLen)
   EndIf
   strArrRow = strArrCol : strDelimCol : strArrRow
   strTempList = strTempList : strDelimRow : strArrRow
Next
strArrList = StrSub (strTempList, 2, -1)

Switch intDirection
Case intDirection
Case @ASCENDING
Case @DESCENDING
   strArrList = ItemSort (strArrList, strDelimRow)
   Continue
Case @DESCENDING
   strTempList = ""
   For intRow = intArrCount To 1 By -1
      strArrRow = ItemExtract (intRow, strArrList, strDelimRow)
      strTempList = strTempList : strDelimRow : strArrRow
   Next
   strArrList = StrSub (strTempList, 2, -1)
   Break
EndSwitch

strTempList = ""
For intRow = 1 To intArrCount
   strArrRow = ItemExtract (intRow, strArrList, strDelimRow)
   strArrRow = ItemRemove (1, strArrRow, strDelimCol)
   strTempList = strTempList : strDelimRow : strArrRow
Next
strArrList = StrSub (strTempList, 2, -1)

FilePut (strFileTemp, strArrList)
arrArray = ArrayFileGetCSV (strFileTemp, 0, strDelimCol) ; "2#a#ii@LF33#b#oo@LF7#c#aa@LF41#d#ee"
FileDelete (strFileTemp)
Return arrArray
;..........................................................................................................................................
; This UDF "udfArraySortDim2" sorts a WinBatch Dim-2 array on the given sort column in ascending or descending mode
; using WinBatch Itemsort function.
;
; (c)Detlev Dalitz.20051205.20100302.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayDisplay (intULx, intULy, intLRx, intLRy, strMsgText, arrData, strDelimCol, strDelimRow)
If strDelimCol == "" Then strDelimCol = @TAB
   Else strDelimCol = StrSub (strDelimCol, 1, 1)
If strDelimRow == "" Then strDelimRow = @LF
   Else strDelimRow = StrSub (strDelimRow, 1, 1)
strTable = "[no data to display]"
intDims = ArrInfo (arrData, 0)
intDim1 = ArrInfo (arrData, 1)
If intDims == 2 && intDim1 > 0
   intILast = intDim1 - 1
   intKLast = ArrInfo (arrData, 2) - 1
   strTable = ""
   For intI = 0 To intILast
      strRow = ""
      For intK = 0 To intKLast
         If IsDefined (arrData[intI, intK]) Then strRow = ItemInsert (arrData[intI, intK], -1, strRow, strDelimCol)
            Else strRow = ItemInsert ("", -1, strRow, strDelimCol)
      Next
      strTable = strTable : strDelimRow : strRow
   Next
   strTable = StrSub (strTable, 2, -1)
EndIf
IntControl (63, intULx, intULy, intLRx, intLRy) ; Set coordinates for AskFileText, AskItemList and AskTextBox windows.
intPrevIC28 = IntControl (28, 0, 0, 0, 0) ; Select System font used in list boxes. p1=1=fixed pitch font. p1=0=proportional font (default)
AskItemlist (strMsgText, strTable, @LF, @UNSORTED, @SINGLE)
:CANCEL
IntControl (28, intPrevIC28, 0, 0, 0)
;..........................................................................................................................................
; This UDF "udfArrayDisplay" displays the content of a WinBatch Dim-2 array using the AskItemList dialog function.
;
; (c)Detlev Dalitz.20051205.20100302.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

DirChange (DirScript ())


; Create array.

strFileIn = "ArrIn.txt"
;   7#c#aa
;   41#d#ee
;   2#a#ii
;   33#b#oo
FilePut (strFileIn, "7#c#aa" : @CRLF : "41#d#ee" : @CRLF : "2#a#ii" : @CRLF : "33#b#oo")

arrArray = ArrayFileGetCSV (strFileIn, 0, "#", 0, 0)
udfArrayDisplay (400, 200, 800, 500, "Array", arrArray, "", "")


; Sort array.

intSortCol = 0
arrArray = udfArraySortDim2 (arrArray, intSortCol, @ASCENDING, "", "")
udfArrayDisplay (400, 200, 800, 500, "Array sorted ascending|Column=" : intSortCol, arrArray, "", "")

arrArray = udfArraySortDim2 (arrArray, intSortCol, @DESCENDING, "", "")
udfArrayDisplay (400, 200, 800, 500, "Array sorted descending|Column=" : intSortCol, arrArray, "", "")


intSortCol = 1
arrArray = udfArraySortDim2 (arrArray, intSortCol, @ASCENDING, "", "")
udfArrayDisplay (400, 200, 800, 500, "Array sorted ascending|Column=" : intSortCol, arrArray, "", "")

arrArray = udfArraySortDim2 (arrArray, intSortCol, @DESCENDING, "", "")
udfArrayDisplay (400, 200, 800, 500, "Array sorted descending|Column=" : intSortCol, arrArray, "", "")


intSortCol = 2
arrArray = udfArraySortDim2 (arrArray, intSortCol, @ASCENDING, "", "")
udfArrayDisplay (400, 200, 800, 500, "Array sorted ascending|Column=" : intSortCol, arrArray, "", "")

arrArray = udfArraySortDim2 (arrArray, intSortCol, @DESCENDING, "", "")
udfArrayDisplay (400, 200, 800, 500, "Array sorted descending|Column=" : intSortCol, arrArray, "", "")


intSortCol = -1 ; Use last column. In this example intSortCol = -1 is the same as intSortCol = 2.
arrArray = udfArraySortDim2 (arrArray, intSortCol, @ASCENDING, "", "")
udfArrayDisplay (400, 200, 800, 500, "Array sorted ascending|Column=" : intSortCol, arrArray, "", "")

arrArray = udfArraySortDim2 (arrArray, intSortCol, @DESCENDING, "", "")
udfArrayDisplay (400, 200, 800, 500, "Array sorted descending|Column=" : intSortCol, arrArray, "", "")

Exit