excel vba - Sort array in VBA -
i have 182.123 size array , want sort them specific attribute of class of array. class called cflujo , property want sort them by string called id_flujo. far i'm doing bubble sort takes long:
sub sort_arreglo(arreglo variant) x = lbound(arreglo) ubound(arreglo) y = x ubound(arreglo) dim aux cflujo aux = new cflujo if ucase(arreglo(y).id_flujo) < ucase(arreglo(x).id_flujo) set aux = arreglo(y) set arreglo(y) = arreglo(x) set arreglo(x) = aux end if next y next x end sub
so far i've researched selection sort know can't delete items array can't make 2 lists sort values 1 other. put data in collection have had trouble regarding quality of data unless alocate memory beforehand (like in array).
there's couple of things can improve execution time:
- load properties in array
- sort pointers instead of objects
- use better algorithm quciksort
with example:
sub sort(arreglo variant) dim cache, vals(), ptrs() long, long redim vals(lbound(arreglo) ubound(arreglo)) redim ptrs(lbound(arreglo) ubound(arreglo)) ' load properties , fill pointers = lbound(arreglo) ubound(arreglo) vals(i) = ucase(arreglo(i).id_flujo) ptrs(i) = next ' sort pointers quicksort vals, ptrs, 0, ubound(vals) ' make copy cache = arreglo ' set value each pointer = lbound(arreglo) ubound(arreglo) set arreglo(i) = cache(ptrs(i)) next end sub private sub quicksort(vals(), ptrs() long, byval i1 long, byval i2 long) dim lo long, hi long, p long, tmp long lo = i1 hi = i2 p = ptrs((i1 + i2) \ 2) while vals(ptrs(lo)) < vals(p): lo = lo + 1: wend while vals(ptrs(hi)) > vals(p): hi = hi - 1: wend if lo <= hi tmp = ptrs(hi) ptrs(hi) = ptrs(lo) ptrs(lo) = tmp lo = lo + 1 hi = hi - 1 end if loop while lo <= hi if i1 < hi quicksort vals, ptrs, i1, hi if lo < i2 quicksort vals, ptrs, lo, i2 end sub
wiki
Comments
Post a Comment