12-28-2006, 09:46 AM
This version can sort descending. Also, array dimensions don't have to be 0-based.
Function SortVariantArray2Dim
Function __SortVarArrAsc
Function __SortVarArrDesc
Function SortVariantArray2Dim
;/
function ARRAY(VARIANT)&a col [flags] ;;flags: 1 descending
;Sorts 2-dim ARRAY(VARIANT).
;a - array variable. Not error if empty.
;col - column index (index in first dimension).
;EXAMPLE
;ARRAY(VARIANT) a.create(3 10) ;;3 columns, 10 rows
;;... (populate)
;SortVariantArray2Dim a 1 ;;sort ascending by second column
if(!a.len) ret
if(a.ndim!=2) end "must be 2-dim array"
int lb1=a.lbound(1)
if(col>a.ubound(1) or col<lb1) end "invalid column index"
int- t_sortArrCol
t_sortArrCol=col-lb1
qsort a.psa.pvData a.len a.len(1)*sizeof(VARIANT) iif(flags&1 &__SortVarArrDesc &__SortVarArrAsc)
Function __SortVarArrAsc
;/
function[c]# VARIANT*a VARIANT*b
;a and b are pointer-based arays containing 1 row
int- t_sortArrCol
ret a[t_sortArrCol].cmp(b[t_sortArrCol])
Function __SortVarArrDesc