Need easy sort function for array...

# Thread: Need easy sort function for array...

1. Senior Member
Join Date
Dec 1969
Posts
1,013

## Need easy sort function for array...

I need a simple sort for about 14 different values... <BR>For Example...<BR><BR>ThisOnesName(1)=BOB<BR>ThisOnesV alue(1)=45<BR><BR>ThatOnesName(2)=JOE<BR>ThatOnesV alue(2)=23<BR><BR>the values are a combination of entries in a database, so I can&#039t do it at the sql level... Any thoughts?

2. Neils Guest

## RE: Need easy sort function for array...

Are these parralel arrays? Meaning, does BOB have to go with 45 and JOD has to go with 23?

3. Senior Member
Join Date
Dec 1969
Posts
1,013

## RE: Need easy sort function for array...

Yes, after I sort I need to be able to label the values with the appropriate name.

4. shaffer Guest

## RE: Need easy sort function for array...

Create a new array, two-dimensional, where the first element of each row is the value from ThisOnesName (e.g. "Bob") and the second element in the row is the original array offset (e.g. "1"). Sort this new array by its first column (see algorithm below, in case you don&#039t already have one). Then, loop through the newly sorted array, using the second column as an offset into both of the original arrays.<BR><BR>Hope this helps!<BR><BR> - Mike<BR><BR>===============<BR> Sub QuickSort(vec,loBound,hiBound,SortField)<BR><BR> &#039::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::<BR> &#039:: Sort a 2 dimensional array on SortField ::<BR> &#039:: ::<BR> &#039:: Parms: ::<BR> &#039:: vec - array to be sorted ::<BR> &#039:: SortField - The field to sort on (2nd dimension value) ::<BR> &#039:: loBound and hiBound are simply the upper and lower ::<BR> &#039:: bounds of the array (generally 0 and ubound-1) ::<BR> &#039::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::<BR><BR> Dim pivot(),loSwap,hiSwap,temp,counter<BR> Redim pivot (Ubound(vec,2))<BR><BR> &#039== Two items to sort<BR> if hiBound - loBound = 1 then<BR> if vec(loBound,SortField) &#062; vec(hiBound,SortField) then Call SwapRows(vec,hiBound,loBound)<BR> End If<BR><BR> &#039== Three or more items to sort<BR> <BR> For counter = 0 to Ubound(vec,2)<BR> pivot(counter) = vec(int((loBound + hiBound) / 2),counter)<BR> vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)<BR> vec(loBound,counter) = pivot(counter)<BR> Next<BR><BR> loSwap = loBound + 1<BR> hiSwap = hiBound<BR> <BR> do<BR> &#039== Find the right loSwap<BR> while loSwap &#060; hiSwap and vec(loSwap,SortField) &#060;= pivot(SortField)<BR> loSwap = loSwap + 1<BR> wend<BR> &#039== Find the right hiSwap<BR> while vec(hiSwap,SortField) &#062; pivot(SortField)<BR> hiSwap = hiSwap - 1<BR> wend<BR> &#039== Swap values if loSwap is less then hiSwap<BR> if loSwap &#060; hiSwap then Call SwapRows(vec,loSwap,hiSwap)<BR><BR><BR> loop while loSwap &#060; hiSwap<BR> <BR> For counter = 0 to Ubound(vec,2)<BR> vec(loBound,counter) = vec(hiSwap,counter)<BR> vec(hiSwap,counter) = pivot(counter)<BR> Next<BR> <BR> &#039== Recursively call function .. the beauty of Quicksort<BR> &#039== 2 or more items in first section<BR> if loBound &#060; (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,SortField)<BR> &#039== 2 or more items in second section<BR> if hiSwap + 1 &#060; hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField)<BR><BR> End Sub &#039QuickSort<BR>

5. Neils Guest

## RE: Need easy sort function for array...

My suggestion would be to use the quicksort that they have on 4guys. You will have to change the code a little where it swaps the array values. When it swaps a value in one array, make sure to add a statement that does the samething to the other array.<BR><BR>Hope this helps.

6. shaffer Guest

## RE: Need easy sort function for array...

Create a new array, two-dimensional, where the first element of each row is the value from ThisOnesName (e.g. "Bob") and the second element in the row is the original array offset (e.g. "1"). Sort this new array by its first column (see algorithm below, in case you don&#039t already have one). Then, loop through the newly sorted array, using the second column as an offset into both of the original arrays. Of course, this would be a lot easier if you had just created the two separate arrays as a single 2-d array to begin with... :)<BR><BR>Hope this helps!<BR><BR> - Mike<BR><BR>===============<BR> Sub QuickSort(vec,loBound,hiBound,SortField)<BR><BR> &#039::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::<BR> &#039:: Sort a 2 dimensional array on SortField ::<BR> &#039:: ::<BR> &#039:: Parms: ::<BR> &#039:: vec - array to be sorted ::<BR> &#039:: SortField - The field to sort on (2nd dimension value) ::<BR> &#039:: loBound and hiBound are simply the upper and lower ::<BR> &#039:: bounds of the array (generally 0 and ubound-1) ::<BR> &#039::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::<BR><BR> Dim pivot(),loSwap,hiSwap,temp,counter<BR> Redim pivot (Ubound(vec,2))<BR><BR> &#039== Two items to sort<BR> if hiBound - loBound = 1 then<BR> if vec(loBound,SortField) &#062; vec(hiBound,SortField) then Call SwapRows(vec,hiBound,loBound)<BR> End If<BR><BR> &#039== Three or more items to sort<BR> <BR> For counter = 0 to Ubound(vec,2)<BR> pivot(counter) = vec(int((loBound + hiBound) / 2),counter)<BR> vec(int((loBound + hiBound) / 2),counter) = vec(loBound,counter)<BR> vec(loBound,counter) = pivot(counter)<BR> Next<BR><BR> loSwap = loBound + 1<BR> hiSwap = hiBound<BR> <BR> do<BR> &#039== Find the right loSwap<BR> while loSwap &#060; hiSwap and vec(loSwap,SortField) &#060;= pivot(SortField)<BR> loSwap = loSwap + 1<BR> wend<BR> &#039== Find the right hiSwap<BR> while vec(hiSwap,SortField) &#062; pivot(SortField)<BR> hiSwap = hiSwap - 1<BR> wend<BR> &#039== Swap values if loSwap is less then hiSwap<BR> if loSwap &#060; hiSwap then Call SwapRows(vec,loSwap,hiSwap)<BR><BR><BR> loop while loSwap &#060; hiSwap<BR> <BR> For counter = 0 to Ubound(vec,2)<BR> vec(loBound,counter) = vec(hiSwap,counter)<BR> vec(hiSwap,counter) = pivot(counter)<BR> Next<BR> <BR> &#039== Recursively call function .. the beauty of Quicksort<BR> &#039== 2 or more items in first section<BR> if loBound &#060; (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1,SortField)<BR> &#039== 2 or more items in second section<BR> if hiSwap + 1 &#060; hibound then Call QuickSort(vec,hiSwap+1,hiBound,SortField)<BR><BR> End Sub &#039QuickSort<BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•