Yep, you did indeed. As always i'm ever so grateful for all your advice and contributions which i've found absolutely invaluable during my short time on this forum (especially with regex!) - didn't mean anything by my comment, just that that was needed for multidimensional array sorting.
On some cursory testing, i now see why you left it out - you can't use this for 1-dimensional arrays as Merge() doesn't evaluate anything in brackets it seems...
Based on this i've revised the code to cater for both 1- and multi-dimensional arrays. Includes a conditional (without pKey as per your code, but also with pKey when needed)... can't get away from at least 1 if statement i guess...
Code: Select all
function sortArray @pArray, pDirection, pSortType, pKey
local tNextIndex, tSortedArray, tSortText
if (pDirection is not in "ascending,descending" and pDirection is not empty) or \
( pSortType is not in "international,numeric,datetime,text,binary" and pSortType is not empty) then
answer "Malformed parameters - sortDirection must be either Ascending, Descending or empty" & return & \
"sortType must be either 'international', 'numeric', 'datetime', 'text', 'binary' or empty"
return pArray
end if
get the keys of pArray
if pKey is empty then
put merge("sort lines of IT [[pDirection]] [[pSortType]] by pArray[each]") into tSortText //1-dimensional array
else
put merge("sort lines of IT [[pDirection]] [[pSortType]] by pArray[each][pKey]") into tSortText //multidimensional array
end if
do tSortText
repeat for each line tKey in IT
add 1 to tNextIndex
put pArray[tKey] into tSortedArray[tNextIndex]
end repeat
return tSortedArray
end sortArray
- Empty pSortDIrection will default to ascending
- Empty pSortType will default to text
- Empty pKey will treat array as 1-dimensional
Thanks once again @Thierry for helping improve my coding