code:
;FUNCTION SortArray()
;
;AUTHOR Jochen Polster (jochen.polster@gmx.net)
; based on Shawn Tassies BubbleUp() UDF
;
;ACTION Sorts the specified Array in the specified Sort order.
;
;SYNTAX SortArray(array,SortOrder,[SortReference],[HeaderRow])
;
;PARAMETERS array (Required)
; - A Variable representing the array to be sorted
;
; SortOrder (Required)
; - An Integer Value representing the Order the
; Array has to be sorted .
; 1 = ascending
; 2 = descending
;
; SortReference (Optional)
; - An Integer Value representing the column of a 2 dimensional
; Array. Must be referred as Arrayelement + 1 :
; $array[n][0] is column 1 !!!
;
; HeaderRow (Optional)
; - If assigned (1) the first 'Row' of a 2 dimensional Array is
; ommited. $array[0] will not get into the Sorting routine !
;
;REMARKS Supports 2 dimensional Arrays (sorting by column).
; Will work also combined Integer/String Arrays, even mixed Values("2a").
; If the specified Variable isn't refering to an Array or there is an
; invalid sort Order specified the Function will set @error to 1 .
;
;RETURNS The Array specified , sorted !
;
;DEPENDENCIES None
;
;EXAMPLES break on cls
; call "path\SortArray.udf"
; $array = 300,"abc","2ab",3,"*",45,"2ac","!","2ad","xxx",76
; $array = SortArray($array,1) ;ascending
; for each $element in $array
; $element ?
; next
; get $
;
; 2 dimensional Array :
;
; break on cls
; call "path\SortArray.udf"
; dim $array[5]
; $array[0] = "Location","Name","Member No."
; $array[1] = "Stuttgart","jpols",611
; $array[2] = "Houston","Bryce",600
; $array[3] = "Toronto","Shawn",173
; $array[4] = "Brisbane","cj",677
; $array[5] = "Jyvaeskylae","Lonkero",2306
; $array = SortArray($array,1,3,1) ;ascending by Member No.($array[x][2]),'header row' excluded
; for $rc = 0 to ubound($array)
; for $col = 0 to ubound($array[$rc])
; at($rc*2,$col*15)$array[$rc][$col]
; next
; next
; get $
function SortArray($array,$Order,optional $Ref,optional $header)
if ubound($array)
dim $Operator
select
case $Order = 1
$Operator = ">"
case $Order = 2
$Operator = "<"
case 1
exit(1)
endselect
if $Ref
if vartype($array[1][$ref-1])
dim $int[ubound($array)]
dim $str[ubound($array)]
dim $h,$i,$j,$str_count,$int_count
dim $offset
if $header $offset = 1 else $offset = 0 endif
for $h = $offset to ubound($array)
for $i = $offset to ubound($array) - 1
$ = execute("if $array[$i][$Ref-1] $Operator $array[$i+1][$Ref-1] $$rc = 1 else $$rc = 0 endif")
if $rc
$j = $array[$i+1] $array[$i+1] = $array[$i] $array[$i] = $j
endif
next
next
for $i = $offset to ubound($array)
if val($array[$i][$Ref-1]) or $array[$i][$Ref-1] = 0
if $array[$i][$Ref-1] = val($array[$i][$Ref-1])
$int[$int_count] = $array[$i] $int_count = $int_count + 1
else
$str[$str_count] = $array[$i] $str_count = $str_count + 1
endif
else
$str[$str_count] = $array[$i] $str_count = $str_count + 1
endif
next
if $int_count and $str_count
for $h = 0 to $str_count - 1
for $i = 0 to $str_count - 2
$ = execute("if $str[$i] $Operator $str[$i+1] $$rc = 1 else $$rc = 0 endif")
if $rc
$j = $str[$i+1] $str[$i+1] = $str[$i] $str[$i] = $j
endif
next
next
if $Order = 1
for $i = 0 to $int_count
$array[$i] = $int[$i]
next
for $i = $int_count to $int_count + $str_count - 1
$array[$i] = $str[$i-$int_count]
next
else
for $h = 0 to $int_count - 1
for $i = 0 to $int_count - 2
$ = execute("if $int[$i] $Operator $int[$i+1] $$rc = 1 else $$rc = 0 endif")
if $rc
$j = $int[$i+1] $int[$i+1] = $int[$i] $int[$i] = $j
endif
next
next
for $i = 0 to $str_count
$array[$i] = $str[$i]
next
for $i = $str_count to $str_count + $int_count - 1
$array[$i] = $int[$i-$str_count]
next
endif
endif
$SortArray = $array
return
else
exit(1)
endif
endif
dim $int[ubound($array)]
dim $str[ubound($array)]
dim $h,$i,$j,$str_count,$int_count
for $h = 0 to ubound($array)
for $i = 0 to ubound($array) - 1
$ = execute("if $array[$i] $Operator $array[$i+1] $$rc = 1 else $$rc = 0 endif")
if $rc
$j = $array[$i+1] $array[$i+1] = $array[$i] $array[$i] = $j
endif
next
next
for $i = 0 to ubound($array)
if val($array[$i]) or $array[$i] = 0
if $array[$i] = val($array[$i])
$int[$int_count] = $array[$i] $int_count = $int_count + 1
else
$str[$str_count] = $array[$i] $str_count = $str_count + 1
endif
else
$str[$str_count] = $array[$i] $str_count = $str_count + 1
endif
next
if $int_count and $str_count
for $h = 0 to $str_count - 1
for $i = 0 to $str_count - 2
$ = execute("if $str[$i] $Operator $str[$i+1] $$rc = 1 else $$rc = 0 endif")
if $rc
$j = $str[$i+1] $str[$i+1] = $str[$i] $str[$i] = $j
endif
next
next
if $Order = 1
for $i = 0 to $int_count
$array[$i] = $int[$i]
next
for $i = $int_count to $int_count + $str_count - 1
$array[$i] = $str[$i-$int_count]
next
else
for $h = 0 to $int_count - 1
for $i = 0 to $int_count - 2
$ = execute("if $int[$i] $Operator $int[$i+1] $$rc = 1 else $$rc = 0 endif")
if $rc
$j = $int[$i+1] $int[$i+1] = $int[$i] $int[$i] = $j
endif
next
next
for $i = 0 to $str_count
$array[$i] = $str[$i]
next
for $i = $str_count to $str_count + $int_count - 1
$array[$i] = $int[$i-$str_count]
next
endif
endif
$SortArray = $array
else
exit(1)
endif
endfunction