Ejemplo:
dim ArrayPrueba(4,1) ArrayPrueba(0,0) = "Pera" : ArrayPrueba(0,1) = 12 ArrayPrueba(1,0) = "Limon" : ArrayPrueba(1,1) = 5 ArrayPrueba(2,0) = "Arroz" : ArrayPrueba(2,1) = 6 ArrayPrueba(3,0) = "Manzana" : ArrayPrueba(3,1) = 8 ArrayPrueba(4,0) = "Leche" : ArrayPrueba(4,1) = 4
Resultado:
Pera = 12
Limon = 5
Arroz = 6
Manzana = 8
Leche = 4
Limon = 5
Arroz = 6
Manzana = 8
Pera = 12
Pera = 12
Limon = 5
Arroz = 6
Manzana = 8
Leche = 4
'Funcion para ordenar ARRAYS
'Parametros:
'vec : nombre de la array
'lobound : principio
'hiBound : maximo
'SortField : campo a ordenar
Call QuickSort(ArrayPrueba,lbound(ArrayPrueba),ubound(ArrayPrueba),1)
Leche = 4Limon = 5
Arroz = 6
Manzana = 8
Pera = 12
Funciones:
'Funcion utilizada para cambiar posiciones en el array
Sub SwapRows(ary,row1,row2)
' This proc swaps two rows of an array
Dim x,tempvar
For x = 0 to Ubound(ary,2)
tempvar = ary(row1,x)
ary(row1,x) = ary(row2,x)
ary(row2,x) = tempvar
Next
End Sub 'SwapRows
'Funcion para ordenar ARRAYS
'Parametros:
'vec : nombre de la array
'lobound : principio
'hiBound : maximo
'SortField : campo a ordenar
Sub QuickSort(vec,loBound,hiBound,SortField)
Dim pivot(),loSwap,hiSwap,temp,counter
Redim pivot (Ubound(vec,2))
' Two items to sort
if hiBound - loBound = 1 then
if vec(loBound,SortField) > vec(hiBound,SortField) then Call SwapRows(vec,hiBound,loBound)
End If
' Three or more items to sort
For counter = 0 to Ubound(vec,2)
pivot(counter) = vec(int((loBound + hiBound) / 2),counter)
vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)
vec(loBound,counter) = pivot(counter)
Next
loSwap = loBound + 1
hiSwap = hiBound
do
' Find the right loSwap
while loSwap < hiSwap and vec(loSwap,SortField) <= pivot(SortField)
loSwap = loSwap + 1
wend
' Find the right hiSwap
while vec(hiSwap,SortField) > pivot(SortField)
hiSwap = hiSwap - 1
wend
' Swap values if loSwap is less then hiSwap
if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap)
loop while loSwap < hiSwap
For counter = 0 to Ubound(vec,2)
vec(loBound,counter) = vec(hiSwap,counter)
vec(hiSwap,counter) = pivot(counter)
Next
' Recursively call function .. the beauty of Quicksort
' 2 or more items in first section
if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,SortField)
' 2 or more items in second section
if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField)
End Sub

